XCTestExpectation 只是挂起

XCTestExpectation just hangs

我不知道发生了什么。我在测试中使用 expectation,它不会超时。我删除了太多测试,现在只剩下:

func testItem() {
    let expec = expectation(description: "expection")
    wait(for: [expec], timeout: 3.0)
}

而且它不会失败。它只是挂起。有谁知道为什么会这样?

如果您使用 XCTestCase.expectation(description:),它将返回的 XCTestExpectation 添加到 self.expectations,您应该使用 waitForExpectations 而不是 wait(for:)

使用 wait(for:) 时,您应该使用 XCTestExpectation(description:) 创建您的期望。