0

I have a strange issue in my testing code. I want to test a BehaviourRelay in my view model changes from status .fetching to .saving. I have the following test code:

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)
        ])
    }
    
}

My Status enum is like so:

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
        }
    }
}

When I run the test I get the following message: XCTAssertEqual failed: ("[next(fetching) @ 0, next(saving) @ 10]") is not equal to ("[next(fetching) @ 0, next(saving) @ 10]")

Cleary these events are equivalent, so why is it failing?

Kex
  • 8,023
  • 9
  • 56
  • 129

1 Answers1

1

I already had a similar issue with you.

In my case, overriding Equatable of BankViewModel.Status make test failed.

Please check. Is there a overriding Implement Equatable protocol in a BankViewModel.Status hierarchy and edit correctly.


Updated from comments

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

Please Match and compare about switch lhs, rhs of Status.saving correctly

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
        }
    }
}
Cruz
  • 2,602
  • 19
  • 29
  • I'm not quite sure what you mean. I have edited my question and added the code for `Status`. I am implementing `Equatable`. Am I missing something? – Kex Sep 27 '20 at 12:41
  • @Kex In your implement Equatable return false if compare .saving == .saving – Cruz Sep 27 '20 at 12:50
  • Thanks for your answer. I missed that – Kex Sep 27 '20 at 13:34