首页 > 解决方案 > XCTestExpectation 应该如何初始化?

问题描述

有 2 个选项如何初始化XCTestExpectation。首先,使用直接初始化程序。

import XCTest

class MyTests: XCTestCase {
    let expectation = XCTestExpectation(description: "foo")`
}

第二个使用XCTest实例方法。

import XCTest

class MyTests: XCTestCase {
    let expectation = self.expectation(description: "foo")
}

两种方法产生相同的结果,所以我想知道哪个更好用?直接XCTestExpectation初始化器会快一点吗?

标签: swiftxctest

解决方案


最大的区别是你想不想用waitForExpectations(timeout: TimeInterval, handler: XCWaitCompletionHandler)

waitForExpectations(timeout: TimeInterval, handler: XCWaitCompletionHandler)只能在使用 . 创建期望时使用self.expectation(description: "foo")

否则,您需要调用wait(for expectations: [XCTestExpectation], timeout seconds: TimeInterval)


推荐阅读