UIAlertController 测试的修正
两个月前,我曾发布了一篇如何测试 UIAlertController的文章。一个读者发现测试没有如期地起作用:
Larhythimx 说的完全正确。模拟程序的初始化方法从来没有调用。为什么我在写这个测试用例的时候没有发觉呢?那是因为 handler 确实被调用了,看起来就像 所以作为黄金通道(即编写不需要改变实现的测试)走不通,那就退而求其次用别的方法。 首先,我们在 extension UIAlertAction { class func makeActionWithTitle(title: String?,style: UIAlertActionStyle,handler: ((UIAlertAction) -> Void)?) -> UIAlertAction { return UIAlertAction(title: title,style: style,handler: handler) } } 在 override class func makeActionWithTitle(title: String?,handler: ((UIAlertAction) -> Void)?) -> MockAlertAction { return MockAlertAction(title: title,handler: handler) } 在实现代码中,我们现在可以使用类方法去创建 alert 动作: let okAction = Action.makeActionWithTitle("OK",style: .Default) { (action) -> Void in self.actionString = "OK" } let cancelAction = Action.makeActionWithTitle("Cancel",style: .Default) { (action) -> Void in self.actionString = "Cancel" } alertViewController.addAction(cancelAction) 为了确保我们的测试用例正常,如我们预期地工作,将 var mockHandler: Handler? 此外,我们为动作的模拟标题添加测试。取消动作的测试应该像这样: func testAlert_FirstActionStoresCancel() { sut.Action = MockAlertAction.self sut.showAlert(UIButton()) let alertController = sut.presentedViewController as! UIAlertController let action = alertController.actions.first as! MockAlertAction action.mockHandler!(action) XCTAssertEqual(sut.actionString,"Cancel") XCTAssertEqual(action.mockTitle,"Cancel") } 这个测试在此前的版本将会失败,因为初始化方法没有被调用,因此模拟标题也没有得到设置。 你可以在 github 上找到修正的版本。 再次感谢 Larhythimx 的推特!
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |