I am trying to test an subclass of UITableViewController
in swift with storyboard. I am able to get a reference to the view controller and log it, but I am unable to typecast it to the class I am trying to test, so I can't access methods, properties etc.
What's the proper way of getting a hold of the instance of my class?
Test Scenario:
import XCTest
import UIKit
class GameListControllerTest: XCTestCase {
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var _sut: AnyObject?
var sut: AnyObject {
if(_sut?) {
return _sut!
}
_sut = storyboard.instantiateViewControllerWithIdentifier("GameListController")
return _sut!
}
override func setUp() {
super.setUp()
println()
println("=================================")
println("sut \(sut)")
println("view \(sut.view)")
println("=================================")
}
func testInstance() {
var vc1 = sut as UITableViewController
var vc2 = sut as? GameListController
println()
println("=================================")
println("VC1 \(vc1)")
println("VC2 \(vc2)")
println("=================================")
}
}
Output:
XCTestOutputBarrierTest Suite '_TtC15mytabletopTests22GameListControllerTest' started at 2014-06-16 06:13:30 +0000
XCTestOutputBarrierTest Case '-[_TtC15mytabletopTests22GameListControllerTest testInstance]' started.
XCTestOutputBarrier
=================================
sut <_TtC10mytabletop18GameListController: 0xb338d50>
view <UITableView: 0xc033800; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0xb344960>; layer = <CALayer: 0xb33c7d0>; contentOffset: {0, 0}; contentSize: {480, 0}>
=================================
=================================
VC1 <_TtC10mytabletop18GameListController: 0xb338d50>
VC2 nil
=================================
Test Case '-[_TtC15mytabletopTests22GameListControllerTest testInstance]' passed (0.007 seconds).
XCTestOutputBarrierTest Suite '_TtC15mytabletopTests22GameListControllerTest' passed at 2014-06-16 06:13:30 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.007 (0.010) seconds
If I try to force typecast (sut as GameListController
) I get an runtime exception.