I'm using Quick library for unit testing.
I'm trying to test ViewController
, view of which is made as XIB
.
I connected view with file owner, and view component with view controller.
And for viewDidLoad
test, I accessed view to trigger viewDidLoad()
.
Here's my code.
override func spec() {
super.spec()
var sut: QuestionViewController!
describe("viewDidLoad") {
afterEach {
sut = nil
}
beforeEach {
sut = QuestionViewController(question: "Q1")
_ = sut.view
}
it("renders question header text") {
expect(sut.headerLabel.text).toEventually(equal("Q1"))
}
}
}
But when run the test, nothing happens with just 'test fail'. I setup break point inside spec(), but it just pass through.(nothing happens)
After removing xib file and make UI components programmatically, the test finally succeeded.
How to test ViewController
(viewDidLoad etc.) that contains xib file when using Quick
?
Probably in your QuestionViewController
init you have similar code to this:
init(question:String) {
//do something with a question
super.init(nibName:"QuestionNibFileName", bundle: nil)
}
And this is working fine in your main target but doesn't work in test, because here default bundle is different. You should use Bundle(for:type(of:self))
so the code should look like this:
init(question:String) {
//do something with a question
let bundle = Bundle(for:type(of:self))
super.init(nibName:"QuestionNibFileName", bundle: bundle)
}