Writing Integration Tests for Your Network Layer Using URLProtocol | by Karthik Shiva | Mar, 2022

smartphone
Photo by Vojtech Bruzek on Unsplash
  1. Create a URLProtocol subclass and implement the above given mandatory methods
  2. Register your URLProtocol subclass with the system in your AppDelegate’s application (_:didFinishLaunchingWithOptions:) method.
  3. Assign the type of your URLProtocol subclass to the protocolClasses property of URLSession. Do not create an instance of your subclass since this is taken care of by the system automatically.

1. Network layer

2. View Model

3. View Controller

startLoading()

RequestHandler

Static properties on URLSessionProxy

  1. contactedURLs: [URL]
  1. Create a view model delegate and fulfill an expectation on the delegate method call, so that we know that its time to check the view model for the mock posts [String]
  2. Configure URLSessionProxy to return a mock response
  3. Call viewDidLoad() on the view model to start the process

Leave a Comment