Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 341
zoul Avatar asked Jun 11 '14 08:06

zoul


People also ask

Can you use CloudKit without Apple Developer?

Yes you will require paid developer account for CloudKit.

How do I use CloudKit dashboard?

Sign in to CloudKit Console at https://icloud.developer.apple.com/. Select the CloudKit Database from the app list. From the container selection drop-down at the top of the page, choose your app's container. Click the Record Types menu item in the Schema section to open the container's schema management page.


1 Answers

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!

like image 198
zoul Avatar answered Oct 15 '22 06:10

zoul