在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处理,这是我测试的目的之一,以确保我不会通过重命名来删除链接,删除连接中的连接故事板编辑器,或者我发现的许多其他方式来破坏事物......
编辑---- 我从一个新项目开始,选择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
}
答案 0 :(得分:14)
我提出了一个更好的解决方案,而不是公开一切。实际上你只需要使用来自测试包的故事板而不是使用nil(强制它来自主要目标的捆绑包)。
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as LoginViewController
vc.loadView()
答案 1 :(得分:0)
感谢@Derrik Hathaway提供解决方案的关键。应用程序和测试目标是不同的模块。
我的应用程序有一个不幸的名称TestingViewControllers
为了解决这个问题,我做了以下更改,并在应用程序代码中将ViewController作为public
类。
添加此行以测试案例:
import TestingViewControllers
将测试用例更改为:
func testExample() {
var storyBoard: UIStoryboard?
var anyVC: AnyObject?
var viewController: TestingViewControllers.ViewController?
storyBoard = UIStoryboard(name:"Main", bundle: nil)
XCTAssert(storyBoard != nil, "Test Not Configured Properly")
anyVC = storyBoard?.instantiateInitialViewController()
viewController = anyVC as? TestingViewControllers.ViewController
XCTAssert(viewController != nil, "Test Not Configured Properly")
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}
测试用例传递
答案 2 :(得分:0)
你是否设置了storyboardID,如果你有,你可以像这样投射它。
您还需要将视图控制器类重命名为例如MyAppViewController使主故事板使用它而不是标准的viewcontroller类然后将storyboardID设置为例如MyappVC
GetMeetingRoomsFreeBusyCached