I understand that you'd like to avoid implicitly unwrapping, but unfortunately (unless you want to go through the trouble of writing your own test runner), you don't have control over which initializer is called when the tests are run.
You could get around this by implicitly unwrapping, as you know, and setting the value of the property in setUp()
, which gets called before every test, so you can be sure the property will not be nil during your tests. This is relatively safe.
class MyTests: XCTestCase {
var viewController: ViewController!
override function setUp() {
super.setUp()
self.viewController = ViewController()
}
}
Alternatively, you could make the property lazy
, which means that it does not have to be implicitly unwrapped. This means that the property does not have to be initialized during object initialization so you don't need to write a new initializer, but will be initialized the first time it is accessed.
class MyTests: XCTestCase {
lazy var viewController: ViewController = {
return ViewController()
}()
}