swift – 无法从XCTest中的UIStoryboard转换自定义UIViewController

前端之家收集整理的这篇文章主要介绍了swift – 无法从XCTest中的UIStoryboard转换自定义UIViewController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Swift,Xcode6-Beta5,我试图单元测试我的“ViewController”,而不是一个非常有创意的名字.

从其他响应中,我想我没有正确配置我的“测试”目标.

失败的测试代码

  1. func testItShouldLoadFromStoryboard() {
  2.  
  3. var storyBoard: UIStoryboard?
  4. var anyVC: AnyObject?
  5. var viewController: ViewController?
  6. var uiViewController: UIViewController?
  7.  
  8. storyBoard = UIStoryboard(name:"Main",bundle: nil)
  9. XCTAssert(storyBoard != nil,"Test Not Configured Properly")
  10.  
  11. anyVC = storyBoard?.instantiateInitialViewController()
  12. viewController = anyVC as? ViewController
  13. // Failing Assertion
  14. XCTAssert(viewController != nil,"Test Not Configured Properly")
  15.  
  16. uiViewController = anyVC as? UIViewController
  17. XCTAssert(uiViewController != nil,"Test Not Configured Properly")
  18.  
  19. }

我可以强制演员如下:

  1. anyVC = storyBoard?.instantiateInitialViewController()
  2. viewController = (anyVC != nil) ? (anyVC as ViewController) : nil

但这导致以下崩溃:

  1. libswiftCore.dylib`swift_dynamicCastClassUnconditional:
  2. 0x10724f5a0: pushq %rbp
  3. 0x10724f5a1: movq %rsp,%rbp
  4. 0x10724f5a4: pushq %r14
  5. 0x10724f5a6: pushq %rbx
  6. 0x10724f5a7: movq %rsi,%rbx
  7. 0x10724f5aa: movq %rdi,%r14
  8. 0x10724f5ad: testq %r14,%r14
  9. 0x10724f5b0: je 0x10724f5de ; swift_dynamicCastClassUnconditional + 62
  10. 0x10724f5b2: movabsq $-0x7fffffffffffffff,%rax
  11. 0x10724f5bc: andq %r14,%rax
  12. 0x10724f5bf: jne 0x10724f5de ; swift_dynamicCastClassUnconditional + 62
  13. 0x10724f5c1: movq %r14,%rdi
  14. 0x10724f5c4: callq 0x107279a6e ; symbol stub for: object_getClass
  15. 0x10724f5c9: nopl (%rax)
  16. 0x10724f5d0: cmpq %rbx,%rax
  17. 0x10724f5d3: je 0x10724f5ed ; swift_dynamicCastClassUnconditional + 77
  18. 0x10724f5d5: movq 0x8(%rax),%rax
  19. 0x10724f5d9: testq %rax,%rax
  20. 0x10724f5dc: jne 0x10724f5d0 ; swift_dynamicCastClassUnconditional + 48
  21. 0x10724f5de: leaq 0x3364d(%rip),%rax ; "Swift dynamic cast Failed"
  22. 0x10724f5e5: movq %rax,0xa456c(%rip) ; gCRAnnotations + 8
  23. 0x10724f5ec: int3
  24. 0x10724f5ed: movq %r14,%rax
  25. 0x10724f5f0: popq %rbx
  26. 0x10724f5f1: popq %r14
  27. 0x10724f5f3: popq %rbp
  28. 0x10724f5f4: retq
  29. 0x10724f5f5: nopw %cs:(%rax,%rax)

我也已经直接成功地实例化了ViewController,但是不做IBOutlet处理,这是我测试的目的之一,确保我不会通过重命名删除链接,在Storyboard编辑器中删除连接,或者我发现打破事情的许多其他方式…

编辑—-
我开始了一个新的项目,选择了iOS应用程序 – >单一查看应用程序模板.

使用如下所示的代码替换testExample,将ViewController添加到测试目标.类似的结果.此模板具有ViewController类型的单个视图控制器,故事板中没有其他任何内容.

  1. func testExample() {
  2.  
  3.  
  4. var storyBoard: UIStoryboard?
  5. var anyVC: AnyObject?
  6. var viewController: ViewController?
  7.  
  8. storyBoard = UIStoryboard(name:"Main",bundle: nil)
  9. XCTAssert(storyBoard != nil,"Test Not Configured Properly")
  10.  
  11. anyVC = storyBoard?.instantiateInitialViewController()
  12. viewController = anyVC as? ViewController
  13. XCTAssert(viewController != nil,"Test Not Configured Properly")
  14.  
  15. // This is an example of a functional test case.
  16. XCTAssert(true,"Pass")
  17.  
  18. }

在anyVC的值之后的断点处设置以下lldb输出

  1. (lldb) po anyVC
  2. (instance_type = Builtin.RawPointer = 0x00007fe22c92a290 -> 0x000000010e20bd80 (void *)0x000000010e20bea0: OBJC_MetaCLASS_$__TtC22TestingViewControllers14ViewController)
  3. {
  4. instance_type = 0x00007fe22c92a290 -> 0x000000010e20bd80 (void *)0x000000010e20bea0: OBJC_MetaCLASS_$__TtC22TestingViewControllers14ViewController
  5. }
我想出了一个比公开的更好的解决方案.您实际上只需要使用来自测试包的故事板,而不是使用nil(强制它来自主要目标的捆绑包).如果有任何人有兴趣再阅读一点,我更加深入 here.
  1. var storyboard: UIStoryboard = UIStoryboard(name: "Main",bundle: NSBundle(forClass: self.dynamicType))
  2. vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as LoginViewController
  3. vc.loadView()

猜你在找的Swift相关文章