问题描述
我写了下面的测试用例,它在swift 1.1中运行良好。但在1.2中它的突破。
I have written the below test case which worked fine in swift 1.1. But in 1.2 its breaking.
class AboutViewController_Tests: XCTestCase
{
//var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType)) // Used in swift 1.1
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:NSBundle.mainBundle()) // Replaced this in swift 1.2
var aboutViewController:AboutViewController!
override func setUp()
{
super.setUp()
aboutViewController = storyboard.instantiateViewControllerWithIdentifier("AboutViewController") as! AboutViewController
aboutViewController.viewDidLoad()
XCTAssertNotNil(aboutViewController, "About not nil")
}
}
运行单元测试时出错
无法转换类型值'testProject.AboutViewController'(0x105b0ad30)到'testProjectTests.AboutViewController'(0x116e51d20)。
我已经做了足够的研究来解决这个问题。但无法做到这一点。我希望你们中的一些人能够解决这个问题并且能够在这里帮助我。
I have done enough research to resolve this issue. But couldn't able to do it. I hope some of you come across this problem and will able to help me here.
推荐答案
我遇到了同样的问题,解决方法是:
I had the same problem and solution is:
- 添加故事板
主
和AboutViewController
in test target - 用$替换
UIStoryboard(名称:Main,bundle:NSBundle.mainBundle())
b $ bUIStoryboard(名称:Main,包:NSBundle(forClass:self.classForCoder))
- Add storyboard
Main
andAboutViewController
in test target - Replace
UIStoryboard(name: "Main", bundle:NSBundle.mainBundle())
withUIStoryboard(name: "Main", bundle: NSBundle(forClass: self.classForCoder))
这样你就可以加载故事板并从测试目标包中初始化控制器,而不是从主目标包中使用它。
This way you'll load storyboard and initialize controller from test target bundle, instead from using it from main target bundle.Check this link for details
这篇关于单元测试从故事板中快速转换视图控制器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!