waitForExpectationsWithTimeout崩溃

时间:2015-07-22 12:21:53

标签: ios swift xctest

我正在尝试使用XCTest测试异步请求,因此使用expectationWithDescription: 然而,当调用waitForExpectationsWithTimeout时,它立即崩溃,甚至没有等待超时。 我甚至尝试不久之后尝试完成执行操作,以排除超时问题,但它并没有改变;这是我的功能:

func testTrafficRefresh(){
    let expectation = expectationWithDescription("refreshed")
    waitForExpectationsWithTimeout(10, handler:nil)
    traffic.trafficRefresh {[weak self] () -> Void in
        let coming=inArrivoHDDetailViewController.sharedDetailController().activeTransit
        let buses=self!.traffic.arrivingBuses
        let count=buses.count
        XCTAssert(count==0 && coming==0, "No buses and active transit")
        XCTAssert(count>0 && coming==1, "Arriving buses and inactive transit")
        expectation.fulfill()   
    }
}

其他功能也会发生同样的行为。如果我拿走waitForExpectationsWithTimeout操作并保持expectationWithDescription操作,它会在函数结束时崩溃。在这两种情况下,崩溃报告如下:

libsystem_kernel.dylib`__pthread_kill:
0x10723227c <+0>:  movl   $0x2000148, %eax
0x107232281 <+5>:  movq   %rcx, %r10
0x107232284 <+8>:  syscall 
0x107232286 <+10>: jae    0x107232290               ; <+20>
0x107232288 <+12>: movq   %rax, %rdi
0x10723228b <+15>: jmp    0x10722dc53               ; cerror_nocancel
0x107232290 <+20>: retq   
0x107232291 <+21>: nop    
0x107232292 <+22>: nop    
0x107232293 <+23>: nop

2 个答案:

答案 0 :(得分:3)

您可以尝试为处理程序提供实现。根据文档handler参数在waitForExpectationsWithTimeout标记中不可选:

func waitForExpectationsWithTimeout(timeout: NSTimeInterval, handler handlerOrNil: XCWaitCompletionHandler!)

因此,您可以尝试提供明确的解包处理程序(即使是空的处理程序也可以):

    waitForExpectationsWithTimeout(10) { error in
        //XCTAssertNil(error, "") this is optional
    }

此外,您可以尝试关注this帖子,看看您是否获得了更合适的崩溃日志。

答案 1 :(得分:2)

我观看了WWDC14中的具体演讲,并提出了以下实现:

 func testTrafficRefresh(){
    let expectation = expectationWithDescription("refreshed")
    traffic.trafficRefresh {[weak self] () -> Void in
        let coming=inArrivoHDDetailViewController.sharedDetailController().activeTransit
        let buses=self!.traffic.arrivingBuses
        let count=buses.count
        XCTAssert(((count == 0 && coming==1)||(count>0 && coming==1)), "Transit status \(coming) not corresponding to arriving buses \(count)")
        expectation.fulfill()
    }
    waitForExpectationsWithTimeout(20, handler:nil)
}

它与原始版本非常相似,但对于waitForExpectationsWithTimeout命令的位置,这似乎是至关重要的。