Unit checks ought to be as freed from exterior dependencies as attainable. Because of this you need to have full management over all the pieces that occurs in your checks.
For instance, for those who’re working with a database, you need the database to be empty or in some predefined state earlier than your check begins. You use on the database throughout your check and after your check the database might be thrown away.
By making your checks not rely on exterior state, you make it possible for your checks are repeatable, can run in parallel and do not rely on one check working earlier than one other check.
Traditionally, one thing just like the community is especially exhausting to make use of in checks as a result of what in case your check runs however you do not have a community connection, or what in case your check runs throughout a time the place the server that you just’re speaking to has an outage? Your checks would now fail despite the fact that there’s nothing improper along with your code. So that you need to decouple your checks from the community in order that your checks turn into repeatable, impartial and run with out counting on some exterior server.
On this submit, I will discover two completely different choices with you.
One possibility is to easily mock out the networking layer solely. The opposite possibility makes use of one thing known as URLProtocol
which permits us to take full management over the requests and responses within URLSession
, which suggests we will really make our checks work with no community connection and with out eradicating URLSession
from our checks.
Defining the code that we need to check
As a way to correctly determine how we will check our code, we should always in all probability outline the objects that we want to check. On this case, I want to check a fairly easy view mannequin and networking pair.
So let’s check out the view mannequin first. This is the code that I want to check for my view mannequin.
@Observable
class FeedViewModel {
var feedState: FeedState = .notLoaded
non-public let community: NetworkClient
init(community: NetworkClient) {
self.community = community
}
func fetchPosts() async {
feedState = .loading
do {
let posts = attempt await community.fetchPosts()
feedState = .loaded(posts)
} catch {
feedState = .error(error)
}
}
func createPost(withContents contents: String) async throws -> Submit {
return attempt await community.createPost(withContents: contents)
}
}
In essence, the checks that I want to write right here would affirm that calling fetchPost
would really replace my checklist of posts as new posts turn into accessible.
Planning the checks
I might in all probability name fetchPost
to make it possible for the feed state turns into a price that I count on, then I might name it once more and return completely different posts from the community, ensuring that my feed state updates accordingly. I might in all probability additionally need to check that if any error could be thrown through the fetching section, that my feed state will turn into the corresponding error kind.
So to boil that right down to an inventory, here is the check I might write:
- Be sure that I can fetch posts
- Be sure that posts get up to date if the community returns new posts
- Be sure that errors are dealt with accurately
I even have the create submit operate, which is just a little bit shorter. It does not change the feed state.
What I might check there’s that if I create a submit with sure contents, a submit with the offered contents is definitely what’s returned from this operate.
I’ve already carried out the networking layer for this view mannequin, so here is what that appears like.
class NetworkClient {
let urlSession: URLSession
let baseURL: URL = URL(string: "https://practicalios.dev/")!
init(urlSession: URLSession) {
self.urlSession = urlSession
}
func fetchPosts() async throws -> [Post] {
let url = baseURL.appending(path: "posts")
let (information, _) = attempt await urlSession.information(from: url)
return attempt JSONDecoder().decode([Post].self, from: information)
}
func createPost(withContents contents: String) async throws -> Submit {
let url = baseURL.appending(path: "create-post")
var request = URLRequest(url: url)
request.httpMethod = "POST"
let physique = ["contents": contents]
request.httpBody = attempt JSONEncoder().encode(physique)
let (information, _) = attempt await urlSession.information(for: request)
return attempt JSONDecoder().decode(Submit.self, from: information)
}
}
In a perfect world, I might be capable of check that calling fetchPosts
on my community shopper is definitely going to assemble the proper URL and that it’s going to use that URL to make a name to URLSession
. Equally for createPost
, I might need to make it possible for the HTTP physique that I assemble is legitimate and incorporates the info that I intend to ship to the server.
There are primarily two issues that we may need to check right here:
- The view mannequin, ensuring that it calls the proper features of the community.
- The networking shopper, ensuring that it makes the proper calls to the server.
Changing your networking layer with a mock for testing
A typical approach to check code that depends on a community is to easily take away the networking portion of it altogether. As an alternative of relying on concrete networking objects, we’d rely on protocols.
Abstracting our dependencies with protocols
This is what that appears like if we apply this to our view mannequin.
protocol Networking {
func fetchPosts() async throws -> [Post]
func createPost(withContents contents: String) async throws -> Submit
}
@Observable
class FeedViewModel {
var feedState: FeedState = .notLoaded
non-public let community: any Networking
init(community: any Networking) {
self.community = community
}
// features are unchanged
}
The important thing factor that modified right here is that as a substitute of relying on a community shopper, we rely on the Networking
protocol. The Networking
protocol defines which features we will name and what the return sorts for these features will likely be.
For the reason that features that we have outlined are already outlined on NetworkClient
, we will replace our NetworkClient
to evolve to Networking
.
class NetworkClient: Networking {
// No modifications to the implementation
}
In our software code, we will just about use this community shopper passage to our feed view mannequin and nothing would actually change. It is a actually low-key approach to introduce testability into our codebase for the feed view mannequin.
Mocking the community in a check
Now let’s go forward and write a check that units up our feed view mannequin in order that we will begin testing it.
class MockNetworkClient: Networking {
func fetchPosts() async throws -> [Post] {
return []
}
func createPost(withContents contents: String) async throws -> Submit {
return Submit(id: UUID(), contents: contents)
}
}
struct FeedViewModelTests {
@Check func testFetchPosts() async throws {
let viewModel = FeedViewModel(community: MockNetworkClient())
// we will now begin testing the view mannequin
}
}
Now that we have now a setup that we will check, it is time to take one other have a look at our testing targets for the view mannequin. These testing targets are what is going on to drive our selections for what we’ll put in our MockNetworkClient
.
Writing our checks
These are the checks that I needed to put in writing for my submit fetching logic:
- Be sure that I can fetch posts
- Be sure that posts get up to date if the community returns new posts
- Be sure that errors are dealt with accurately
Let’s begin including them one-by-one.
As a way to check whether or not I can fetch posts, my mock community ought to in all probability return some posts:
class MockNetworkClient: Networking {
func fetchPosts() async throws -> [Post] {
return [
Post(id: UUID(), contents: "This is the first post"),
Post(id: UUID(), contents: "This is post number two"),
Post(id: UUID(), contents: "This is post number three")
]
}
// ...
}
With this in place, we will check our view mannequin to see if calling fetchPosts
will really use this checklist of posts and replace the feed state accurately.
@Check func testFetchPosts() async throws {
let viewModel = FeedViewModel(community: MockNetworkClient())
await viewModel.fetchPosts()
guard case .loaded(let posts) = viewModel.feedState else {
Difficulty.document("Feed state will not be set to .loaded")
return
}
#count on(posts.depend == 3)
}
The second check would have us name fetchPosts
twice to make it possible for we replace the checklist of posts within the view mannequin.
To ensure that us to regulate our checks absolutely, we should always in all probability have a approach to inform the mock community what checklist of posts it ought to return after we name fetchPost
. Let’s add a property to the mock that permits us to specify an inventory of posts to return from inside our checks:
class MockNetworkClient: Networking {
var postsToReturn: [Post] = []
func fetchPosts() async throws -> [Post] {
return postsToReturn
}
func createPost(withContents contents: String) async throws -> Submit {
return Submit(id: UUID(), contents: contents)
}
}
And now we will write our second check as follows:
@Check func fetchPostsShouldUpdateWithNewResponses() async throws {
let shopper = MockNetworkClient()
shopper.postsToReturn = [
Post(id: UUID(), contents: "This is the first post"),
Post(id: UUID(), contents: "This is post number two"),
Post(id: UUID(), contents: "This is post number three")
]
let viewModel = FeedViewModel(community: shopper)
await viewModel.fetchPosts()
guard case .loaded(let posts) = viewModel.feedState else {
Difficulty.document("Feed state will not be set to .loaded")
return
}
#count on(posts.depend == 3)
shopper.postsToReturn = [
Post(id: UUID(), contents: "This is a new post")
]
await viewModel.fetchPosts()
guard case .loaded(let posts) = viewModel.feedState else {
Difficulty.document("Feed state will not be set to .loaded")
return
}
#count on(posts.depend == 1)
}
The check is now extra verbose however we’re in full management over the responses that our mock community will present.
Our third check for fetching posts is to make it possible for errors are dealt with accurately. Because of this we should always apply one other replace to our mock. The purpose is to permit us to outline whether or not our name to fetchPosts
ought to return an inventory of posts or throw an error. We are able to use End result
for this:
class MockNetworkClient: Networking {
var fetchPostsResult: End result<[Post], Error> = .success([])
func fetchPosts() async throws -> [Post] {
return attempt fetchPostsResult.get()
}
func createPost(withContents contents: String) async throws -> Submit {
return Submit(id: UUID(), contents: contents)
}
}
Now we will make our fetch posts calls succeed or fail as wanted within the checks. Our checks would now have to be up to date in order that as a substitute of simply passing an inventory of posts to return, we will present success with the checklist. This is what that might appear like for our first check (I’m certain you possibly can replace the longer check primarily based on this instance).
@Check func testFetchPosts() async throws {
let shopper = MockNetworkClient()
shopper.fetchPostsResult = .success([
Post(id: UUID(), contents: "This is the first post"),
Post(id: UUID(), contents: "This is post number two"),
Post(id: UUID(), contents: "This is post number three")
])
let viewModel = FeedViewModel(community: shopper)
await viewModel.fetchPosts()
guard case .loaded(let posts) = viewModel.feedState else {
Difficulty.document("Feed state will not be set to .loaded")
return
}
#count on(posts.depend == 3)
}
Information that we will present a hit or failure for our checks. We are able to really go on forward and inform our checks to throw a selected failure.
@Check func fetchPostsShouldUpdateWithErrors() async throws {
let shopper = MockNetworkClient()
let expectedError = NSError(area: "Check", code: 1, userInfo: nil)
shopper.fetchPostsResult = .failure(expectedError)
let viewModel = FeedViewModel(community: shopper)
await viewModel.fetchPosts()
guard case .error(let error) = viewModel.feedState else {
Difficulty.document("Feed state will not be set to .error")
return
}
#count on(error as NSError == expectedError)
}
We now have three checks that check our view mannequin.
What’s attention-grabbing about these checks is that all of them rely on a mock community. Because of this we’re not counting on a community connection. However this additionally doesn’t suggest that our view mannequin and community shopper are going to work accurately.
We’ve not examined that our precise networking implementation goes to assemble the precise requests that we count on it to create. As a way to do that we will leverage one thing known as URLProtocol
.
Mocking responses with URLProtocol
Understanding that our view mannequin works accurately is basically good. Nevertheless, we additionally need to make it possible for the precise glue between our app and the server works accurately. That implies that we ought to be testing our community shopper in addition to the view mannequin.
We all know that we should not be counting on the community in our unit checks. So how can we remove the precise community from our networking shopper?
One method may very well be to create a protocol for URLSession
and stuff all the pieces out that manner. It is an possibility, however it’s not one which I like. I a lot choose to make use of one thing known as URLProtocol
.
Once we use URLProtocol
to mock out our community, we will inform URLSession
that we ought to be utilizing our URLProtocol
when it is attempting to make a community request.
This enables us to take full management of the response that we’re returning and it implies that we will make it possible for our code works with no need the community. Let’s check out an instance of this.
Earlier than we implement all the pieces that we want for our check, let’s check out what it seems wish to outline an object that inherits from URLProtocol
. I am implementing a few primary strategies that I’ll want, however there are different strategies accessible on an object that inherits from URLProtocol
.
I extremely advocate you check out Apple’s documentation for those who’re enthusiastic about studying about that.
Establishing ur URLProtocol subclass
For the checks that we have an interest implementing, that is the skeleton class that I will be working from:
class NetworkClientURLProtocol: URLProtocol {
override class func canInit(with request: URLRequest) -> Bool {
return true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
return request
}
override func startLoading() {
// we will carry out our pretend request right here
}
}
Within the startLoading
operate, we’re presupposed to execute our pretend community name and inform the shopper (which is a property that we inherit from URLProtocol
) that we completed loading our information.
So the very first thing that we have to do is implement a manner for a person of our pretend community to offer a response for a given URL. Once more, there are a lot of methods to go about this. I am simply going to make use of probably the most primary model that I can provide you with to make it possible for we do not get slowed down by particulars that can range from venture to venture.
struct MockResponse {
let statusCode: Int
let physique: Information
}
class NetworkClientURLProtocol: URLProtocol {
// ...
static var responses: [URL: MockResponse] = [:]
static var validators: [URL: (URLRequest) -> Bool] = [:]
static let queue = DispatchQueue(label: "NetworkClientURLProtocol")
static func register(
response: MockResponse, requestValidator: @escaping (URLRequest) -> Bool, for url: URL
) {
queue.sync {
responses[url] = response
validators[url] = requestValidator
}
}
// ...
}
By including this code to my NetworkClientURLProtocol
, I can register responses and a closure to validate URLRequest
. This enables me to check whether or not a given URL
ends in the anticipated URLRequest
being constructed by the networking layer. That is significantly helpful once you’re testing POST
requests.
Notice that we have to make our responses and validators objects static. That is as a result of we will not entry the precise occasion of our URL protocol that we will use earlier than the request is made. So we have to register them statically after which in a while in our begin loading operate we’ll pull out the related response invalidator. We have to make it possible for we synchronize this by means of a queue so we have now a number of checks working in parallel. We’d run into points with overlap.
Earlier than we implement the check, let’s full our implementation of startLoading
:
class NetworkClientURLProtocol: URLProtocol {
// ...
override func startLoading() {
// be certain that we're good to...
guard let shopper = self.shopper,
let requestURL = self.request.url,
let validator = validators[requestURL],
let response = responses[requestURL]
else {
Difficulty.document("Tried to carry out a URL Request that does not have a validator and/or response")
return
}
// validate that the request is as anticipated
#count on(validator(self.request))
// assemble our response object
guard let httpResponse = HTTPURLResponse(
url: requestURL,
statusCode: response.statusCode, httpVersion: nil,
headerFields: nil
) else {
Difficulty.document("Not in a position to create an HTTPURLResponse")
return
}
// obtain response from the pretend community
shopper.urlProtocol(self, didReceive: httpResponse, cacheStoragePolicy: .notAllowed)
// inform the URLSession that we have "loaded" information
shopper.urlProtocol(self, didLoad: response.physique)
// full the request
shopper.urlProtocolDidFinishLoading(self)
}
}
The code incorporates feedback on what we’re doing. Whilst you won’t have seen this type of code earlier than, it ought to be comparatively self-explanatory.
Implementing a check that makes use of our URLProtocol subclass
Now that we’ve bought startLoading
carried out, let’s try to use this NetworkClientURLProtocol
in a check…
class FetchPostsProtocol: NetworkClientURLProtocol { }
struct NetworkClientTests {
func makeClient(with protocolClass: NetworkClientURLProtocol.Kind) -> NetworkClient {
let configuration = URLSessionConfiguration.default
configuration.protocolClasses = [protocolClass]
let session = URLSession(configuration: configuration)
return NetworkClient(urlSession: session)
}
@Check func testFetchPosts() async throws {
let networkClient = makeClient(with: FetchPostsProtocol.self)
let returnData = attempt JSONEncoder().encode([
Post(id: UUID(), contents: "This is the first post"),
Post(id: UUID(), contents: "This is post number two"),
Post(id: UUID(), contents: "This is post number three"),
])
let fetchPostsURL = URL(string: "https://practicalios.dev/posts")!
FetchPostsProtocol.register(
response: MockResponse(statusCode: 200, physique: returnData),
requestValidator: { request in
return request.url == fetchPostsURL
},
for: fetchPostsURL
)
let posts = attempt await networkClient.fetchPosts()
#count on(posts.depend > 0)
}
}
The very first thing I am doing on this code is creating a brand new subclass of my NetworkClientProtocol
. The rationale I am doing that’s as a result of I might need a number of checks working on the identical time.
For that motive, I would like every of my Swift check features to get its personal class. This may be me being just a little bit paranoid about issues overlapping by way of when they’re known as, however I discover that this creates a pleasant separation between each check that you’ve and the precise URLProtocol
implementation that you just’re utilizing to carry out your assertions.
The purpose of this check is to make it possible for once I ask my community shopper to go fetch posts, it really performs a request to the proper URL. And given a profitable response that incorporates information in a format that’s anticipated from the server’s response, we’re in a position to decode the response information into an inventory of posts.
We’re primarily changing the server on this instance, which permits us to take full management over verifying that we’re making the proper request and now have full management over regardless of the server would return for that request.
Testing a POST request with URLProtocol
Now let’s see how we will write a check that makes certain that we’re sending the proper request after we’re attempting to create a submit.
struct NetworkClientTests {
// ...
@Check func testCreatePost() async throws {
let networkClient = makeClient(with: CreatePostProtocol.self)
// arrange anticipated information
let content material = "It is a new submit"
let expectedPost = Submit(id: UUID(), contents: content material)
let returnData = attempt JSONEncoder().encode(expectedPost)
let createPostURL = URL(string: "https://practicalios.dev/create-post")!
// register handlers
CreatePostProtocol.register(
response: MockResponse(statusCode: 200, physique: returnData),
requestValidator: { request in
// validate primary setup
guard
let httpBody = request.streamedBody,
request.url == createPostURL,
request.httpMethod == "POST" else {
Difficulty.document("Request will not be a POST request or does not have a physique")
return false
}
// guarantee physique is right
do {
let decoder = JSONDecoder()
let physique = attempt decoder.decode([String: String].self, from: httpBody)
return physique == ["contents": content]
} catch {
Difficulty.document("Request physique will not be a sound JSON object")
return false
}
},
for: createPostURL
)
// carry out community name and validate response
let submit = attempt await networkClient.createPost(withContents: content material)
#count on(submit == expectedPost)
}
}
There’s numerous code right here, however general it follows a fairly related step to earlier than. There’s one factor that I need to name your consideration to, and that’s the line the place I extract the HTTP physique from my request within the validator. As an alternative of accessing httpBody
, I am accessing streamedBody
. This isn’t a property that usually exists on URLRequest
, so let’s discuss why I would like that for a second.
Whenever you create a URLRequest
and execute that with URLSession
, the httpBody
that you just assign is transformed to a streaming physique.
So once you entry httpBody
within the validator closure that I’ve, it is going to be nil
.
As an alternative of accessing that, we have to entry the streaming physique, collect the info, and return alll information.
This is the implementation of the streamedBody
property that I added in an extension to URLRequest
:
extension URLRequest {
var streamedBody: Information? {
guard let bodyStream = httpBodyStream else { return nil }
let bufferSize = 1024
let buffer = UnsafeMutablePointer.allocate(capability: bufferSize)
var information = Information()
bodyStream.open()
whereas bodyStream.hasBytesAvailable {
let bytesRead = bodyStream.learn(buffer, maxLength: bufferSize)
information.append(buffer, depend: bytesRead)
}
bodyStream.shut()
return information
}
}
With all this in place, I will now test that my community shopper constructs a totally right community request that’s being despatched to the server and that if the server responds with a submit like I count on, I am really in a position to deal with that.
So at this level, I’ve checks for my view mannequin (the place I mock out your entire networking layer to make it possible for the view mannequin works accurately) and I’ve checks for my networking shopper to make it possible for it performs the proper requests on the right instances.
In Abstract
Testing code that has dependencies is all the time just a little bit difficult. When you’ve a dependency you may need to mock it out, stub it out, take away it or in any other case cover it from the code that you just’re testing. That manner you possibly can purely check whether or not the code that you just’re enthusiastic about testing acts as anticipated.
On this submit we checked out a view mannequin and networking object the place the view mannequin is dependent upon the community. We mocked out the networking object to make it possible for we may check our view mannequin in isolation.
After that we additionally needed to put in writing some checks for the networking object itself. To do this, we used a URLProtocol
object. That manner we may take away the dependency on the server solely and absolutely run our checks in isolation. We are able to now check that our networking shopper makes the proper requests and handles responses accurately as properly.
Because of this we now have end-to-end testing for a view mannequin and networking shopper in place.
I don’t usually leverage URLProtocol
in my unit checks; it’s primarily in advanced POST
requests or flows that I’m enthusiastic about testing my networking layer this deeply. For easy requests I are inclined to run my app with Proxyman hooked up and I’ll confirm that my requests are right manually.