How to organize mock files with SPM

Sergey Petrachkov
2 min readDec 9, 2021

--

In this article, I’m gonna share my thoughts about how to organize mock files within a swift package.

When you work with Swift package manager you don’t have xcodeproj most of the time. Even so, SPM is a powerful tool and what I like about it is that one can fully cover the package with tests and use it as a stand-alone thing, without linking it to a project with Xcodeproj and stuff.

Unit tests are easy. But what if you need mock files with JSONs? How to read those files and how to make sure they are not copied to your main app bundle during integration?

Let’s get straight to it! Let’s create a Mocks directory within the Tests target and add a few JSON files there.

Now let’s exclude them in the package manifest.

And a quick tip on how to read the mocks. Add this file to your tests target.

And now you can read your mock files like that in your tests or in any MockClass within the test target. And those JSONs (there can be plenty of them) won’t be copied to your main target and the app bundle.

Enjoy!

--

--

Sergey Petrachkov

Amsterdam-based iOS developer born and bred in Siberia. Follow my "Metal in the Attic" Youtube channel :)