【发布时间】:2014-12-07 20:46:22
【问题描述】:
我正在尝试在 Swift 中执行一些非常简单的特性/功能测试,但我有一些疑问需要解决才能创建有用的测试。
我想验证另一个控制器呈现的控制器是否存在于应用程序导航层次结构中(控制器是否已呈现到 NavigationController 中并不重要,如模态或其他)。
如果我以编程方式直接在测试函数中实例化和显示控制器,当我检查 On Top 控制器 时,我总是得到 Storyboard 根控制器而不是我刚刚实例化,好像我手动创建的控制器从未添加到应用程序层次结构中。
这里是伪代码示例:
func testController(){
// Instantiate a controller
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
let controller1 = storyBoard.instantiateViewControllerWithIdentifier("Controller1") as? ControllerOneViewController
controller1.loadView()
// Call a function that instantiates another controller
controller1.pushAnotherController()
// Test that the current shown controller is what we expect...
let rootController = UIApplication.sharedApplication().keyWindow?.rootViewController
XCTAssert(rootController.self == TheExpectedClass, "Controller is not what we expect")
}
【问题讨论】:
-
你应该展示 controller1.pushAnotherController() 的作用。您的帖子未显示可疑类应属于哪种类型。
-
此信息不相关。它们可以是任何类型的控制器并以不同的方式呈现
-
我说检查问题可能出在哪里是相关的。如果你说它不相关,你应该发布一个更通用的解决方案,比如直接在这个块中推送另一个控制器。恕我直言
标签: testing swift functional-testing