我试过在异步环境中使用XCTKeyPathExpectation,但无法让它工作。我得到的错误让我很困惑。因为在我看来,所陈述的错误并不是真正的错误...
我创建了一个非常简单的测试来检查我是否做错了什么。使用下面两个类:
TestMock.swift:
import Foundation
@testable import UnitTests
final class TestMock: NSObject {
@objc private(set) var testCalled: Bool = false
func test() {
self.testCalled = true
}
}
UnitTestsTests.swift:
import XCTest
@testable import UnitTests
final class UnitTestsTests: XCTestCase {
var testMock: TestMock!
override func setUpWithError() throws {
self.testMock = TestMock()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
self.testMock = nil
try super.tearDownWithError()
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
let expectation = XCTKeyPathExpectation(keyPath: \TestMock.testCalled,
observedObject: self.testMock,
expectedValue: true)
self.testMock.test()
self.wait(for: [expectation], timeout: 1.0)
}
}
它给了我错误:测试示例():异步等待失败:已超过1秒的逾时,未达成预期:“需要〈单元测试.测试模拟:的'Swift.引用可写密钥路径〈单元测试.测试模拟,Swift.布尔值〉'的值0x 600003 bfc 090〉为“真”,则为“真”“。
我唯一能想到的就是比较Swift.Bool和Objective-C的Bool,但不知道如何解决这个问题。
1条答案
按热度按时间hfyxw5xn1#
看起来我需要在testCalled的声明中添加“dynamic”一词。