NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Testing: Mocking out classes by default

Forums > SwiftUI


I'm writing up a swift app, and at present, I'm mocking out things such as keychain and network requests in each test file. If I forget to stub/mock any particular file, it'll make a live call, which I'd rather avoid.

I assume there's a standard practice for overriding network/keychain functionality by default at the target level, but I'm unsure what it is and, frustratingly, can't seem to google the right term.

Anyone have pointers on where to look to understand how to stub out certain functionality for all of a test target?


@JB is mocking us:

I'm mocking out things such as keychain and network requests.... Anyone have pointers on where to look to understand how to stub out certain functionality for all of a test target?

Not sure I really grasp the question. But it reminded me of an excellent pointer I found watching one of Stewart Lynch's videos.

Watch -> Mock Data (Skip to minute 9:00).

In this video he codes an Initializer for his data model class with a boolean Preview parameter. When set to true, the initializer uses mock data from a file in his application. Otherwise, the data is loaded from a URL source. Setting the parameter to true eliminates a live call to an external webservice.

In your app, you might consider adding a toggle to a preferences view to switch between live calls and mock data. While developing add an icon to your main view showing the data is coming from your mock source ⛔️, or the live view ✅.

Re-reading your post, it seems you may have already tried this approach? I am not clear. Stewart makes this decision at the data model level, rather than the target. I may have delivered a botched response to your question...

Keep coding!

Please return and let us know how you solved this. Your solution will be useful to many of us!


Hi @Obelix. Thanks for the response, and apologies for the hazy question. Admittedly I wrote the above late at night - I'll try clarify with an example:

At present, if I want to mock out network functionality, in each specific test I'll do something like:

objectBeingTested.networkDependency = MockNetworkHandler()

I have to include that line in every test (or at least, in the setupWithErrors() for every test) I want to run.

Given I'm never going to want to run tests on the live network, I'm looking for a way to ensure that, for all tests in my test suite, that MockNetworkHandler is automatically set. I'm not sure what the convention is for doing that.

I'm guessing extending the networking class within the test target might be an option, but just adding a file that extends the network class doesn't seem to get picked up in the test suite.

As for adding network code to the actual (non-test) code: I'm happy to do that, but my experience from other languages makes the idea of putting test logic into non-test code a little nerve-inducing. Is that considered a kosher practice in Swift?


Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.