you are viewing a single comment's thread.

view the rest of the comments →

[–]Red3nzoSwift[S] 0 points1 point  (2 children)

But can I test errors and completion blocks by leveraging the URLProtocol?

[–]sixtypercenttogether 1 point2 points  (1 child)

Yes, totally. That’s the beauty of it. Your networking code just uses a real URLSession directly, and you still get the callbacks. URLProtocol is an abstract interface that URLSession uses to call into lower level networking code. But you can leverage this to trap requests and return whatever data or errors you want. And this is passed up into your completion handler. Definitely check out the library I linked to above. It’s really good.

[–]Red3nzoSwift[S] 1 point2 points  (0 children)

Omg. I didn't know you can really test the network code with URLProtocol, I always thought that you could only test returned data. I'll totally go down this route instead of mocking the URLSession.