【问题标题】:XCTestCase waitForExpectationsWithTimeout:handler: throwing EXC_BAD_ACCESS when expectation is not fulfilledXCTestCase waitForExpectationsWithTimeout:handler: 当期望不满足时抛出 EXC_BAD_ACCESS
【发布时间】:2015-02-19 20:27:21
【问题描述】:

我正在使用 XCTestExpectation 测试异步调用。

当completionHandler在给定的1秒超时之前执行时,以下代码有效(测试成功)。

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,这不是很方便,因为这使得不可能看到整个测试套件结果.

我怎样才能避免这种情况并获得正常的测试失败?

【问题讨论】:

    标签: ios xcode swift xctest xctestexpectation


    【解决方案1】:

    似乎导致 EXC_BAD_ACCESS 的原因是在创建期望时传递了一个 nil 描述。

    将任何字符串传递给此调用使其工作,当期望未满足时,我们会得到预期的测试失败。

    let expectation: XCTestExpectation = self.expectationWithDescription("...")
    

    【讨论】:

    • 当我假设处理程序块参数error 从来不是nil 时遇到了类似的错误。无论成功还是失败,处理程序实际上都会被调用。因此,您必须在这种情况下检查错误。
    • 设置描述并不能解决我的问题。我对我的所有期望进行了描述,但它们仍然偶尔会由于未实现的期望而崩溃。
    • 设置描述也不能解决我的问题:\
    猜你喜欢
    • 2016-05-27
    • 2012-05-11
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    相关资源
    最近更新 更多