# Debounce unit test

**URL:** <https://forums.kodeco.com/t/debounce-unit-test/162432>\
**Category:** Combine: Asynchronous Programming with Swift\
**Created:** [January 17, 2022, 8:27pm UTC](https://forums.kodeco.com/t/debounce-unit-test/162432 "2022-01-17T20:27:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon48102836](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon48102836](https://forums.kodeco.com/u/anon48102836)\
**Post date:** [January 17, 2022, 8:27pm UTC](https://forums.kodeco.com/t/debounce-unit-test/162432/1 "2022-01-17T20:27:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [January 18, 2022, 9:40am UTC](https://forums.kodeco.com/t/debounce-unit-test/162432/2 "2022-01-18T09:40:48Z")

</div>

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.](https://github.com/pointfreeco/combine-schedulers#immediatescheduler)

---

<div class="post-metadata">

**Author:** ![anon48102836](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon48102836](https://forums.kodeco.com/u/anon48102836)\
**Post date:** [January 18, 2022, 9:44am UTC](https://forums.kodeco.com/t/debounce-unit-test/162432/3 "2022-01-18T09:44:37Z")

</div>

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:

```auto
        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.

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [January 18, 2022, 10:05am UTC](https://forums.kodeco.com/t/debounce-unit-test/162432/4 "2022-01-18T10:05:50Z")

</div>

That would mostly work if you have just a few unit tests 👍🏼
