Closed esetnik closed 8 years ago
Anyone have any suggestions about how I can help debug this? it looks like Quick is just not working for new projects created in Xcode 7.1 / Swift 2
I have a theory: can you try importing UIKit at the top of your test file?
// LoginViewControllerSpec.swift
import Quick
import Nimble
+import UIKit
@testable import ExampleApp
// ...
This reminds me of other issues that have been reported, and those users were able to resolve their problems by importing UIKit.
Unfortunately I get the same error even when I import UIKit
. It's important to note that the test passes without any build errors if I use a UIViewController whereas fail with the error above if I use my custom UIViewController subclass.
Compiles and Passes:
import Quick
import Nimble
@testable import SumuApp
class LoginViewControllerSpec: QuickSpec {
override func spec() {
var viewController: UIViewController!
beforeEach {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
viewController = storyboard.instantiateViewControllerWithIdentifier(
"LoginViewControllerID")
}
describe(".viewDidLoad()") {
beforeEach {
// Method #1: Access the view to trigger LoginViewController.viewDidLoad().
let _ = viewController.view
}
}
}
}
Compilation Failure With Aformentioned Error
import Quick
import Nimble
@testable import SumuApp
class LoginViewControllerSpec: QuickSpec {
override func spec() {
var viewController: LoginViewController!
beforeEach {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
viewController = storyboard.instantiateViewControllerWithIdentifier(
"LoginViewControllerID") as! LoginViewController
}
describe(".viewDidLoad()") {
beforeEach {
// Method #1: Access the view to trigger LoginViewController.viewDidLoad().
let _ = viewController.view
}
}
}
}
My custom UIViewController subclass is nothing special as far as I can tell
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var logoLabel: UILabel!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillDisappear(animated: Bool) {
navigationController?.setNavigationBarHidden(false, animated: animated)
super.viewWillDisappear(animated)
}
}
I think that the “Enable Testability” build setting for you app target is not set to Yes
, since the internal LoginViewController
is not exposed even if you are using @testable import
; that would not be a Quick's issue.
And also you should check that if this happens with plain XCTestCase
to separate the root cause.
I have Enable Testability
enabled in my app target.
I also confirmed that I can reproduce this with a plain XCTestCase
so I guess this is not a Quick issue rather an Xcode 7.1 bug.
I received a response from Apple regarding this issue.
Apple Developer Relations04-Nov-2015 04:13 PM
This issue behaves as intended based on the following:
UI tests run outside your app in a separate process. You can’t access app code from inside a UI test - that’s intentionally part of the design. Use unit testing for tests that need to access the app code and UI tests to automate user interaction testing.
We are now closing this bug report.
So the solution is to move your view controller tests from theUITests
target to the Tests
target.
Thanks for the closing the loop @esetnik! :+1:
Using XCode 7.1 with a brand new swift-based project. I am trying to create a test for
UIViewController
subclass referenced in a storyboard.Installed Quick 0.8.0 with first Cocoapds and then Carthage following the installation instructions.
I receive the following error when trying to run my project tests:
LoginViewControllerSpec.swift in ExampleAppUITests Module
LoginViewController.swift in ExampleApp Module
override func viewDidLoad() { super.viewDidLoad()
}