Let's see how we can write unit test for code that uses the Combine framework. We need to first use dependency injection to replace URLSession data task publisher with a Result publisher in the mock service.
I will write a few tests to show you different test cases and use Combine operators like collect, first, output(at). You can find the full list of Combine operators at the official Apple documentation here:
https://developer.apple.com/documenta...
You need to scroll down a lot. Some of the operators have examples. They don't show a good use case but are a good example for implementation details.
overview:
00:00 intro
01:17 dependency injection
07:19 writing tests with Combine publishers
20:41 testing error cases
22:22 dependency injection with the Result publisher
29:05 testing if data stream completes
34:24 wrap up
⬇️ you can find the project at my Github https://github.com/gahntpo/Transformi...
The other parts of this series are:
Part 1 transforming operators: map, tryMap and compactMap • Combine framework tutorial: transform...
Part 2 switchToLatest and flatMap • Combine framework tutorial - Part 2 -...
Part 3: error handling in Combine • Combine framework tutorial - Part 3 -...
Part 4: unit testing with dependency injection • Combine framework tutorial - Part 4 -...
If you liked what you learned and you want to see more, check out one of my courses!
👩🏻💻 Combine course https://school.swiftyplace.com/course...
#CombineFramework #UnitTesting