Intermediate Combine

Apr 13 2021 · Swift 5.3, macOS 11.1, Xcode 12.2

Part 1: Intermediate Combine

06. Testing Combine Operators

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Retrying and Catching Errors Next episode: Part 1 Quiz: Intermediate Combine

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Testing is an important part of development for all apps. You can write tests against your Combine code, and in particular, a few operators. You'll use a pattern to organize your test logic. The Given-When-Then pattern. Given a condition, when an action is performed, then an expected result occurs. Let's dive straight into a demo and see how we can test collect and flatMap. Add a subscriptions property to store subscriptions in and set it to an empty array in tearDown. Now walk through the pattern. Given a condition is first. Given the values 0, 1 and 2 in an array and a publisher from that array. When an action is performed, when collect operates on the publisher, and sink subscribes to the collect operator. Within that sink, the expected result occurs. XCTAssert ensures that the collected output equals the passing values. To run this test, run the playground and check the console for output. If you were running this test in an Xcode project, you could run the test by clicking on the diamond next to test_collect. The project will build and run the test. And the diamond will turn to a green check mark when it passes. To make sure that the test is correct, you can change the assert condition to something you know will fail. Run the test again, and you'll get a failure along with a message stating what was expected versus what was actually received from the test. We'll focus on passing tests for the rest of the episode. As you may know, from other Combine courses the flatMap operator can be used to flatten multiple upstream publishers into a single publisher. And you can optionally specify the max number of publishers to receive and flatten. To test this, add a test_flatMapWithMax2Publisher's method to the test class and define a type alias for IntPublisher to represent a passthrough subject that emits integer values and never remits an error. This represent the start of the Given portion of our test cycle. Then define three IntPublisher subjects. Make a publisher that is a current value subject that emits values of type IntPublisher and never emits an error passing in the first subject. Now define the expected values and initialize an array for the results. Initialize the Combine pipeline. Call flatMap on the publisher specifying you want at max two publishers. Then attach a sink subscriber that appends the value receive to the array. Now for the, When part of the testing pattern. Send values to the IntPublisher publishers and interweave sending IntPublisher to the main publisher. Send completion event when you're done. Since the publisher is a current value subject, it will replay the current value to any new subscribers. Note that after the third subject is passed in, a value is sent to the third subject, then to the second subject. Since flatMap only takes in a max of two publishers, that third publisher will get ignored. Now for the Then part of the testing pattern. Assert that the results match the expected values or print an error message if they don't match. Run the test, you'll see the test pass since the third publisher is ignored. And the values 1, 2 and 4 are received by the sink.