ios – XCTestCase waitForExpectationsWithTimeout:handler:
发布时间:2020-12-14 19:38:21 所属栏目:百科 来源:网络整理
导读:我正在使用XCTestExpectation测试异步调用. 在给定的1秒超时之前执行completionHandler时,以下代码有效(测试成功). func test__async_call() { // prepare let sut = ClassToTest() let expectation: XCTestExpectation = self.expectationWithDescription(n
我正在使用XCTestExpectation测试异步调用.
在给定的1秒超时之前执行completionHandler时,以下代码有效(测试成功). func test__async_call() { // prepare let sut = ClassToTest() let expectation: XCTestExpectation = self.expectationWithDescription(nil) // test sut.methodToTestWithCompletionHandler() { () -> () in expectation.fulfill() } // verify self.waitForExpectationsWithTimeout(1,handler: nil) } 但是,如果没有调用completionHandler,因此没有满足期望,而是在调用waitForExpectationsWithTimeout时没有得到测试失败,我得到一个EXC_BAD_ACCESS,这不是很方便,因为这使得无法看到整个测试套件的结果. 如何避免这种情况并获得正常的测试失败? 解决方法
似乎导致EXC_BAD_ACCESS的原因是在创建期望时传递nil描述.
将任何字符串传递给此调用使其工作,并且在未满足期望时我们得到预期的测试失败. let expectation: XCTestExpectation = self.expectationWithDescription("...") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |