Testable Network Layer in Swift - Part 3

Please check the previous articles on Network Layer Part 1 and Part 2.

Mocking URLProtocol

What is Mocking?

In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. (source: wiki)

In our case, we are looking to mimic a URLSession so that it will return the mock object without hitting the physical server. We can achieve this by creating MockURLProtocol that conforms to URLProtocol and implements its required methods. Now our MockURLProtocol will be capable of handling the specified request.

[caption id="attachment_12538" align="alignnone" width="1000"]

Testable Network Layer

MockURLProtocol[/caption]

Now we need to add our MockURLProtocol to the URLSessionConfiguration.protocolClasses array to handle requests in a session.

[caption id="attachment_12539" align="alignnone" width="897"]

Testable Network Layer

Custom URLSessionConfiguration[/caption]

Mocking in Action:

For example, when mocking the loginUser function, we will prepare a fake response object that mimics what we expect on a real server call. This response object will be returned as a response from the requestHandler of the MockURLProtocol.

[caption id="attachment_12540" align="alignnone" width="984"]

https://cdn-images-1.medium.com/max/1000/1*ghji23GlQXkfAqhsn0PBlg.png

Actual implementation for reference[/caption]

[caption id="attachment_12541" align="alignnone" width="939"]

Testable Network Layer in Swift

Fake response object[/caption]

The final mockLoginUser function will look like this:

[caption id="attachment_12542" align="alignnone" width="1000"]

Testable Network Layer

Mock loginUser function[/caption]

Now check the APIMockingTests.swift file in the sample code.

Then check updates in the APIHandler.swift file, as updated code using Builder Pattern and refactored BaseRequest, AuthRequest with RequestBuilder.

Run the tests again. After refactoring the code, we observed that all the tests passed without changing the test cases. The advantage of testing is that we can guarantee that all cases are handled even after refactoring.

You can find the complete source code below:

appitventures/IOS-Blog
To share files related to blogs which can be made public — appitventures/IOS-Bloggithub.com

This is based on WWDC 2018: “Testing Tips & Tricks”

link: https://developer.apple.com/videos/play/wwdc2018/417/

Award-winning custom app developers

Inc. Power Partner 2022 Award Badge
Colorado Companies to Watch Winner 2022 Badge
Denver Business Journal 40 Under 40 2022 CEO Amanda Moriuchi Award Badge
Top B2B Companies Clutch Colorado 2021 Badge
Fast 50 Inno On Fire Award Badge
Denver Business Journal 2021 Best Places to Work Award Badge
Top Mobile App Developer Featured on UpCity Award Badge
Expertise Best Mobile App Developer Badge
Top Flutter App Developers  Award Badge
Let's build something awesome.

Reach out to us to discuss building a custom app for your company. We'll get back to you within 24 business hours.

Send us a message
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.