# Testing Cancelling Effect

**URL:** <https://forums.swift.org/t/testing-cancelling-effect/37909>\
**Category:** Swift Composable Architecture\
**Created:** [June 25, 2020, 3:38pm UTC](https://forums.swift.org/t/testing-cancelling-effect/37909 "2020-06-25T15:38:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![atimca](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atimca/32/11764_2.png) [@atimca](https://forums.swift.org/u/atimca)\
**Post date:** [June 25, 2020, 3:38pm UTC](https://forums.swift.org/t/testing-cancelling-effect/37909/1 "2020-06-25T15:38:06Z")

</div>

Hello! Could somebody help me with a problem? Right now I have to cancel one Effect here is my code:

```swift
        case .goNext:
            return .cancel(id: WordAudioPlayerCancelId())

```

This event called when the module did all work and before destroying I need to cancel word audio effect.  
Unfortunately, I cannot find a solution to test this code. Nothing mutates State, nothing sends new Actions.

I really don't want to left any floating and untested code in the Reducer. Especially when this code prevents me from the crash) I delete this module after goNext Action was send and I need to cancel any Events in progress.

---

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [June 25, 2020, 5:41pm UTC](https://forums.swift.org/t/testing-cancelling-effect/37909/2 "2020-06-25T17:41:41Z")

</div>

If you comment out your `.cancel` effect do you get a failing test?

I would expect that the test fails unless you cancel the effect because then you would have a currently running effect, and the store `.assert` requires that all effects be completed. So, the mere fact that your test passes means that you have properly torn down that effect.

If that's not the case can you provide some code showing how the effect is started and how you are writing your test?

If you want super fine-grained assertions of precisely when effects are completed, you could use `.handleEvents` on your mock effect in order to tap into the exact moment it is canceled:

```swift
let mockEffect = PassthroughSubject<...>()
var didComplete = false

let store = TestStore( 
  initialState: ...,
  reducer: ...,
  environment: .mock(
    effect: mockEffect
      .handleEvents(receivedCompletion: { _ in didComplete = true })
      .eraseToEffect()
  )
)

store.assert(
  // [send an action to start the effect]

  .send(.goNext) { 
    ...
    XCTAssertTrue(didComplete)
  }
)

```

---

<div class="post-metadata">

**Author:** ![atimca](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atimca/32/11764_2.png) [@atimca](https://forums.swift.org/u/atimca)\
**Post date:** [June 26, 2020, 9:48am UTC](https://forums.swift.org/t/testing-cancelling-effect/37909/3 "2020-06-26T09:48:54Z")

</div>

Thank you for the fast answer @mbrandonw. I don't know why I didn't get this way of testing by myself) 🙃

I've managed to write a proper test, thank you again for the help!

```swift
func testAudioCancelation() {
    // Given
    let store = TestStore(
        initialState: ListenAndTranslateState(
            word: WordBuilder().build(),
            options: [],
            selectedOption: nil,
            isAudioPlaying: false
        ),
        reducer: ListenAndTranslateState.reducer,
        environment: environment
    )
    // When
    var didComplete = false
    store.assert(
        .environment {
            $0.wordPlayer = WordAudioPlayer { _ in
                Empty<(), Never>(completeImmediately: false)
                    .map { _ in () }
                    .setFailureType(to: Never.self)
                    .handleEvents(receiveCancel: { didComplete = true })
                    .eraseToEffect()
            }
        },
        .send(.startAudio) {
            $0.isAudioPlaying = true
        },
        .send(.goNext(.null)),
        .do { XCTAssertTrue(didComplete) }
    )
}

```
