iOS Swift: Unit Testing Combine Publisher

Saad El Oulladi
3 min readOct 20, 2023

iOS development has undergone significant changes since the introduction of SwiftUI and Combine. Reactive programming has become a standard practice, as Combine is included in the iOS SDK by default.

Unit testing reactive code can sometimes be challenging, as the inputs and outputs are not easily obtained as in the case of imperative programming.

The boring approach

The most common way to test a Combine publisher is to create an expectation and fulfill it once the value is received, as shown below:

While using expectations to test a Combine publisher is intuitive, it comes with some drawbacks:

  1. It involves writing a lot of code, for what is supposed to be a relatively simple test case.
  2. When an expected value is not received, having an expectation with a timeout means waiting for the expectation to fail, which can increase the overall execution time of unit tests, potentially leading to longer test durations.
  3. The use of expectations with timeouts…

--

--