XCTestCase waitForExpectationsWithTimeout:handler: throwing EXC_BAD_ACCESS when expectation is not fulfilled

南笙酒味 提交于 2019-12-10 12:56:47

问题


I am testing an asynchronous call using XCTestExpectation.

The following code works(the test succeeds) when the completionHandler is executed before the given 1 second timeout.

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)
    }

However, if the completionHandler is not called, and therefore the expectation not fulfilled, instead of getting an test failure when calling waitForExpectationsWithTimeout I get an EXC_BAD_ACCESS, which is not very handy since this makes it impossible to see the whole test suite results.

How can I avoid this and get a normal test failure?


回答1:


Seems that what is causing the EXC_BAD_ACCESS is passing a nil description when creating the expectation.

Passing any string to this call makes it work and we get the expected test failure when the expectation is not fulfilled.

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


来源:https://stackoverflow.com/questions/27590830/xctestcase-waitforexpectationswithtimeouthandler-throwing-exc-bad-access-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!