Debounce unit test

Hello,

Let’s suppose we have a a string as a published property in our view model. We subscribe to this published property in our view model and use different combine operators. One of these operators is debounce. When unit testing I cannot get my values to be emitted. How would we mock / bypass debounce when unit testing?

Thanks in advance!

To control time while executing Combine code you need a custom scheduler (you always need to pass one when calling functions like debounce or throttle).

The guys at pointfree have a few very useful schedulers in a repo, here you can read how to address the exact situation you’re describing: GitHub - pointfreeco/combine-schedulers: ⏰ A few schedulers that make working with Combine more testable and more versatile.

Hello,

Thanks for the reply. The way I solved it is by adding a delay and expectation. My debounce runs on dispatch queue main so in my tests I did:

        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            expectation.fulfill()
        }

        // Then
        waitForExpectations(timeout: 1, handler: nil)

Assertions go here

Not sure if this is a good way but it unblocked me.

That would mostly work if you have just a few unit tests :+1:t3: