我正在尝试对我的应用程序进行单元测试,并且大多数测试失败,原因是异步等待失败:超过了30秒的超时,并没有实现期望:“本地代码”。
我不知道为什么它会失败,但这是下面的代码
class HomeTest: XCTestCase {
override func setUp() {
}
override func tearDown() {
}
func testHome() {
let expec = expectation(description: "Home Code")
let presenter = HomePresenter(view: HomeTestVC(expectation: expec), source: Repository.instance)
presenter.start()
wait(for: [expec], timeout: 30)
}
func testPerformanceExample() {
self.measure {
}
}
}
class HomeTestVC: HomeContract.View {
func showRatingForLastTrip(_ trip: Trip) {}
func setProgress(enabled: Bool) {}
func didFail(message: String) {}
func didShowError(error: Error) {}
func didShowStatusCode(code: Int?) {
XCTAssertGreaterThan(200, code ?? 0)
self.expec.fulfill()
}
var expec: XCTestExpectation
init(expectation: XCTestExpectation) {
self.expec = expectation
}
}
它会弹出模拟器,但仅停留在第一个屏幕上。我不知道为什么。任何帮助将不胜感激
答案 0 :(得分:0)
您没有实现您的期望
func testExample() {
let expec = expectation(description: "Home Code")
someAsyncTask() { _ in
expectation.fulfill()
}
wait(for: [expec], timeout: 30)
}
请参见Testing Asynchronous Operations with Expectations
注意:
答案 1 :(得分:0)
您需要fulfill
期望。像这样:
let expectation = self.expectation(description: "Alert")
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0, execute: {
expectation.fulfill()
})
waitForExpectations(timeout: 5, handler: nil)
XCTAssert(whatever)