Working with Subjects in Combine

In this blog post, we explore the concept of subjects in the Combine framework. We’ll look at two types provided by Apple: CurrentValueSubject and PassthroughSubject. We’ll explore their differences and when to use them. What are Subjects? Subject is a type of publisher in the Combine framework that adheres to the Subject protocol. It has a .send(_:) method that allows developers to send specific values to a subscriber. Subjects put values into a stream....

Using URLProtocol to Mock and Test

When writing unit tests for Swift networking code that uses URLSession we don’t want to perform real network requests. We want to mock responses so that our unit tests remain fast, predictable and independent. Mocking responses from URLSession can be done by wrapping it in a protocol and creating a mock session when testing. But an alternative way is using URLProtocol. What is URLProtocol? URLProtocol is a class that allows you to intercept and handle network requests, giving you the ability to implement features such as caching, redirects, or authentication....

February 1, 2023 · Mike Gopsill ·  Testing