WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Unit Testing of CloudKit Using Mock Objects

Forums > Swift

I'm pretty new to Swift and app development, but I do have some history of software engineering. And while working on Paul's excellent long form project, Ultimate Portfolio App, I kept hearing the distant call of the "test first" mantra of Test-driven development (TDD) (Extreme Programming, if you are old enough to recall ;-).

I started to dig into ways of testing CloudKit, and came up with more questions than answers - including this one, which I think was on the right track. The problem is, CloudKit doesn't make itself easy to test. The main interface to the API, CKContainer, has init methods that cannot be overridden or overloaded. So drop-in mocks are out of the question. But to make a long story shorter, I tried many paths to test CloudKit and came up with a framework that works well for me - and I hope that it will for you too.

I call it the MockCloudKitFramework - and its fully documented (using DocC) and tested (see the accompanying project called "MockCloudKitTestProject" in the same repository). Its available here. It doesn't mock every CloudKit functionality by any means. But it does mock enough functionality to make it quite useful. And the framework can be extended to add more CloudKit functional mocking, so please let me know what you think and if you have ideas to extend it.


Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.