Please check the previous articles on Network Layer Part 1 and Part 2.
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.
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.
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.
Did you know that 88% of spreadsheets contain errors? While spreadsheets can be an effective tool for businesses, they propose serious risks. Our free eBook outlines the pros and cons of using spreadsheets to run your mission-critical operations. Download the ebook and start automating your unique workflows.
Pros of Spreadsheets
Cons of Spreadsheets (It's not what you think)
Next Step Solutions and How to Get Started
Bonus Case Study
Thank you! Enjoy our Healthcare Case Study!
Oops! Something went wrong while processing your request. Please try again or Contact Us for assistance.