我的测试代码中有一个奇怪的问题。我想BehaviourRelay
在我的视图模型中测试从 status.fetching
到.saving
. 我有以下测试代码:
class BankViewModelTests: XCTestCase {
private var scheduler: TestScheduler!
private var bag: DisposeBag!
private var user: UserModel!
override func setUpWithError() throws {
try! super.setUpWithError()
let url = Bundle(for: type(of: self)).url(forResource: "User", withExtension: "json")!
let jsonData = try! Data(contentsOf: url)
let jsonDict = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! JSONDictionary
user = UserModel(jsonDict)!
scheduler = TestScheduler(initialClock: 0)
bag = DisposeBag()
}
override func tearDownWithError() throws {
user = nil
scheduler = nil
bag = nil
try! super.tearDownWithError()
}
}
extension BankViewModelTests {
func testSavingStatus() {
// Arrange
let sut = BankViewModel(user: user)
let status = scheduler.createObserver(BankViewModel.Status.self)
sut.status.bind(to: status).disposed(by: bag)
// Action
scheduler.createColdObservable([.next(10, ())]).bind(to: sut.tappedSubmit).disposed(by: bag)
scheduler.start()
// Assert
XCTAssertEqual(status.events, [
.next(0, .fetching),
.next(10, .saving)
])
}
}
我的Status
枚举是这样的:
enum Status: Equatable {
case fetching, fetchSuccess, saving, saveSuccess, failure(Error)
public static func == (lhs: Status, rhs: Status) -> Bool {
switch (lhs, rhs) {
case (.fetching, .fetching),
(.fetchSuccess, .fetchSuccess),
(.saving, .saveSuccess),
(.failure, .failure):
return true
default: return false
}
}
}
当我运行测试时,我收到以下消息:XCTAssertEqual failed: ("[next(fetching) @ 0, next(saving) @ 10]") is not equal to ("[next(fetching) @ 0, next(saving) @ 10]")
显然这些事件是等价的,那么为什么会失败呢?