我在快速使用单元测试方面还很陌生,当我尝试创建用于测量性能的单元测试时,我遇到了错误。
这是我的测试用例的代码。
func testPerformaceFetchSearchResult(){
//let ICD9ViewController = ICD9Controller() //this is not working since i need the IBOutlet also
var ICD9ViewController:ICD9Controller!
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:NSBundle(forClass: self.dynamicType))
ICD9ViewController = storyboard.instantiateViewControllerWithIdentifier("ICD9Controller") as! ICD9Controller
ICD9ViewController.beginAppearanceTransition(true, animated: false)
ICD9ViewController.code = "0"
ICD9ViewController.name = ""
ICD9ViewController.desc = ""
ICD9ViewController.sortBy = 1
ICD9ViewController.customPage.pageNo = 1
ICD9ViewController.customPage.pageSize = 10
self.measureBlock(){
ICD9ViewController.fetchSearchResult()
}
}
错误在这一行
ICD9ViewController = storyboard.instantiateViewControllerWithIdentifier("ICD9Controller") as! ICD9Controller
错误信息是
Could not cast value of type 'appName.ICD9Controller' (address) to 'appName.ICD9Controller' (differentAddress)
我已经阅读了几个关于 XCTestCase 的问题,大部分错误是无法将 _appName_.viewController
类型的值转换为 _appNameTests_.viewController
这对我来说不是这种情况因为我已经检查了 Storyboard和 Controller 的目标成员资格。
我的问题是,如何正确地从 Storyboard 中实例化 View Controller 以进行单元测试?
最佳答案
View Controller 和 Storyboard应该只在应用程序目标上可用,然后您可以使用 @testable
在您的测试中访问它。
检查这 post对你也有帮助。另外,如果这不起作用,请尝试使用 NSBundle.mainBundle()
而不是测试包。
关于ios - 从 Storyboard 中实例化 View Controller 时出现 XCTestCase 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34734096/