Search code examples
iostestingcloudkit

Running CloudKit code in tests?


I’d like to write automated tests for my CloudKit code using the XCTest framework. The test case runs, gets a valid container object, gets a valid public database object, but then dies with an exception when I try to do anything real:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
    reason: 'Failed to establish valid container/bundleID information'

What I have tried so far:

  • I’ve checked the Info.plist for my test target and the bundle ID there corresponds to the container ID.
  • I have swizzled [[NSBundle mainBundle] bundleIdentifier] to make sure it returns the correct bundle ID.
  • I have copied the iCloud entitlements from my main app’s target.

But I still get the exception. Is there a way to get the CloudKit code running in tests?


Solution

  • The main point is running the tests as “application tests”. I test a custom framework, so there’s no application to run the test suite in, no entitlements and therefore no CloudKit. I had to add a simple “Test Rig” target (an empty Cocoa app), set the entitlements there and set this test rig as a Bundle Loader and a Test Host for the test suite. Then the tests can use CloudKit just fine. Thank you for getting me on the right track, @Ward!