简体   繁体   English

单元测试从故事板中快速转换视图控制器不起作用

[英]Unit Test Swift- casting view controller from storyboard not working

I have written the below test case which worked fine in swift 1.1. 我写了下面的测试用例,它在swift 1.1中运行良好。 But in 1.2 its breaking. 但在1.2中它的突破。

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")
}
}

Error while running the unit test 运行单元测试时出错

Could not cast value of type 'testProject.AboutViewController' (0x105b0ad30) to 'testProjectTests.AboutViewController' (0x116e51d20). 无法将'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: 我有同样的问题,解决方案是:

  • Add storyboard Main and AboutViewController in test target 在测试目标中添加storyboard MainAboutViewController
  • Replace UIStoryboard(name: "Main", bundle:NSBundle.mainBundle()) with UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.classForCoder)) UIStoryboard(name: "Main", bundle:NSBundle.mainBundle())替换UIStoryboard(name: "Main", bundle:NSBundle.mainBundle()) UIStoryboard(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 请查看此链接了解详情

I faced the same problem a few minutes ago. 几分钟前我遇到了同样的问题。 Here's how i solved it. 这是我如何解决它。

  1. Add storyboard to the testing target 将故事板添加到测试目标
  2. Load the view controller this way: 以这种方式加载视图控制器:
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:NSBundle(forClass: self.dynamicType))

self.vc = storyboard.instantiateViewControllerWithIdentifier("gettingStartedView") as! MainViewController

self.vc.loadView()

Hope this helps! 希望这可以帮助!

Try this it worked 试试这个有效

class VehicleListControllerSpecs: XCTestCase {

var listController: VehicleListController!

override func setUp() {
    super.setUp()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "VehicleListController") as! VehicleListController
listController = vc
    _ = listController.view
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

func testListViewHasTableView() {
    XCTAssertNotNil(listController.tableView,"view doesnt has tableview")
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查看标签栏 controller 从 storyboard swift 5 - view tab bar controller from storyboard swift 5 对从情节提要加载的视图控制器中的私有属性进行单元测试 - Unit testing a private property in view controller loaded from the storyboard 从故事板连接到Swift视图控制器文件的UITextField输入 - Input from UITextField connected from storyboard to Swift view controller file Swift:从我介绍的情节提要视图控制器中调用函数? - Swift: call function from storyboard view controller I presented? XCTest Xcode 7 Beta 3 Swift 2从情节提要发行版启动View Controller - XCTest Xcode 7 beta 3 swift 2 initiate View Controller from storyboard issue 基于故事板的iOS项目中Swift类的弹出视图控制器 - Pop view controller from Swift class in storyboard based iOS project 使用导航控制器在 Storyboard 中显示视图控制器 - Swift - Present View Controller in Storyboard with a Navigation Controller - Swift 从视图即按即按视图控制器无法快速运行 - Push To view controller from View is not working in swift Swift故事板列表视图不起作用? - Swift Storyboard List view Not Working? 初始情节提要视图控制器未加载Swift 3 - Initial storyboard view controller not loading swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM