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

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

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

失败的测试代码

func testItShouldLoadFromStoryboard() {

    var storyBoard: UIStoryboard?
    var anyVC: AnyObject?
    var viewController: ViewController?
    var uiViewController: UIViewController?

    storyBoard = UIStoryboard(name:"Main",bundle: nil)
    XCTAssert(storyBoard != nil,"Test Not Configured Properly")

    anyVC = storyBoard?.instantiateInitialViewController()
    viewController = anyVC  as? ViewController
    // Failing Assertion 
    XCTAssert(viewController != nil,"Test Not Configured Properly")

    uiViewController = anyVC as? UIViewController
    XCTAssert(uiViewController != nil,"Test Not Configured Properly")

  }

我可以强制演员如下:

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

但这导致以下崩溃:

libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10724f5a0:  pushq  %rbp
0x10724f5a1:  movq   %rsp,%rbp
0x10724f5a4:  pushq  %r14
0x10724f5a6:  pushq  %rbx
0x10724f5a7:  movq   %rsi,%rbx
0x10724f5aa:  movq   %rdi,%r14
0x10724f5ad:  testq  %r14,%r14
0x10724f5b0:  je     0x10724f5de               ; swift_dynamicCastClassUnconditional + 62
0x10724f5b2:  movabsq $-0x7fffffffffffffff,%rax
0x10724f5bc:  andq   %r14,%rax
0x10724f5bf:  jne    0x10724f5de               ; swift_dynamicCastClassUnconditional + 62
0x10724f5c1:  movq   %r14,%rdi
0x10724f5c4:  callq  0x107279a6e               ; symbol stub for: object_getClass
0x10724f5c9:  nopl   (%rax)
0x10724f5d0:  cmpq   %rbx,%rax
0x10724f5d3:  je     0x10724f5ed               ; swift_dynamicCastClassUnconditional + 77
0x10724f5d5:  movq   0x8(%rax),%rax
0x10724f5d9:  testq  %rax,%rax
0x10724f5dc:  jne    0x10724f5d0               ; swift_dynamicCastClassUnconditional + 48
0x10724f5de:  leaq   0x3364d(%rip),%rax       ; "Swift dynamic cast Failed"
0x10724f5e5:  movq   %rax,0xa456c(%rip)       ; gCRAnnotations + 8
0x10724f5ec:  int3   
0x10724f5ed:  movq   %r14,%rax
0x10724f5f0:  popq   %rbx
0x10724f5f1:  popq   %r14
0x10724f5f3:  popq   %rbp
0x10724f5f4:  retq   
0x10724f5f5:  nopw   %cs:(%rax,%rax)

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

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

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

func testExample() {


      var storyBoard: UIStoryboard?
      var anyVC: AnyObject?
      var viewController: ViewController?

      storyBoard = UIStoryboard(name:"Main",bundle: nil)
      XCTAssert(storyBoard != nil,"Test Not Configured Properly")

      anyVC = storyBoard?.instantiateInitialViewController()
      viewController = anyVC as? ViewController
      XCTAssert(viewController != nil,"Test Not Configured Properly")

      // This is an example of a functional test case.
      XCTAssert(true,"Pass")

    }

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

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

猜你在找的Swift相关文章