0

我的测试代码中有一个奇怪的问题。我想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]")

显然这些事件是等价的,那么为什么会失败呢?

4

1 回答 1

1

我已经和你有过类似的问题。

就我而言,重写 BankViewModel.Status 的 Equatable使测试失败。

请检查。BankViewModel.Status层次结构中是否有一个重写的实现 Equatable 协议并正确编辑。


从评论更新

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), // <- (.saving == .saving) always false so test make fail
             (.failure, .failure):
            return true
        default: return false
        }
    }
}

XCTAssertEqual(Status.saving, Status.saving) // Now, It should be failed because overriding Equatable implement

请正确匹配和比较 Status.saving 的开关 lhs, rhs

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, .saving),
             (.saveSuccess, .saveSuccess),
             (.failure, .failure):
            return true
        default:
            return false
        }
    }
}
于 2020-09-27T12:34:55.353 回答