Search code examples
iosswiftunit-testingcloudkitxctest

How to set the value of partialErrorsByItemID on CKError for testing?


I want to test my error handling for a class handling CloudKit Operations. I'd like to be able to construct a CKError with a particular partial error, so that I can pass it into the Operation's completion block under test.

I can init a CKError with the .partialError code but as the partialErrorsByItemID Dictionary is get-only, I can't set the partial errors.

let error = CKError(.partialError)
error.partialErrorsByItemID = ["TestItemID": CKError(.zoneNotFound)] // => Cannot assign to property: 'partialErrorsByItemID' is a get-only property

Is there a way to control the value of partialErrorsByItemID?


Solution

  • You can assign the value of partialErrorsByItemID by providing the relevant key/value pair in the userInfo Dictionary, and using the init(_:userInfo:) initializer:

    let error = CKError(.partialFailure, userInfo: [
        CKPartialErrorsByItemIDKey: ["TestItemID": CKError(.zoneNotFound)]
    ])