Replies: 2 comments 2 replies
-
Hey,
You can install it as a Swift package from WeTransfer/Mocker. It's embedded in the project to make the dependency private. I briefly looked into the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In my application, I have a single class
DefaultNetworkService
that handles all the network requests. (About the name:DefaultNetworkService
conforms to aNetworkService
protocol, so it can be mocked when testing other parts of the app.)In general, I've tried to leave most of the app's logic out of
DefaultNetworkService
, but inevitably some has crept into it, and it would be nice to write tests for it.Worth noting it has a
client: Get.APIClient
property. My initial thought was to write a protocol thatGet.APIClient
already conforms to, and inject a mock client. The mock client was feeling like it was going to be a lot of work (even though it really only needs thesend
function).Instead I took inspiration from the tests in the
Get -> Tests -> GetTests
folder, specifically theAPIClient
extension inHelpers.swift
.I quickly made my own
APIClient
extension, but then realized that it's this line that was doing the magic there:I was tempted to include the whole
Mocker
folder in my project, but I couldn't find a way to do that without copying it in. (Would be curious to hear if anyone knows if that is possible.) Instead I decided I could get away with just a customized version ofMockingURLProtocol
. Mine looks like this:This is working great, except that, because it's relying on static properties, my tests can't run in parallel. I tried to think of a way to key the canned
responseValue
andrequestError
s, but just didn't find a good way to do it. Particularly if (as I do), I want to check forrequestedUrlStrings
after a request. (Essentially, some of my API requests kick off other requests, and this is some of the logic I'm testing for.)For now, I've just decided that all my network tests will happen in one (rather large) test case. It's not great, but it's way better than no tests at all.
Curious if anyone has any feedback on this approach, or thoughts on how to avoid the monolithic test case situation. I'm happy to post more examples if anyone is interested in seeing more of the code.
Beta Was this translation helpful? Give feedback.
All reactions