Search code examples
swiftunit-testingobservablerx-swift

In RxSwift, how can I unit test that an observable sent no event?


I am new to testing un RxSwift. I manage to test that an Observable emits values with RxBlocking. But I can't find the syntax to make a test pass if an Observable did not send any value.

I think I should use toBlocking(timeout: ),but I can't find how (I have an error that it timed out, but the test fail).

Thank you for your help


Solution

  • Does your observable complete? It looks like you're testing to see if the observable emits any events not values

    Assuming you are testing on the XCTest Framework,

    1. If you are testing to see if the observable emits no elements before it completes,
    XCTAssertNil(try observable.toBlocking().first())
    
    1. If you are testing to see if the observable does not emit any elements, nor completes
    // Check error if necessary
    XCTAssertThrowsError(try observable.toBlocking(timeout: timeout).first())