Search code examples
iosunit-testingswiftios8cloudkit

Mocking CKContainer for Unit Testing


I am using CloudKit in my application and am trying to mock CKContainer to test my Managers. Here is what i tried:

    func testAccountStatus() {

class MockCloudContainer: CKContainer {

  override func accountStatusWithCompletionHandler(completionHandler: ((CKAccountStatus, NSError!) -> Void)!)
  {
    completionHandler(CKAccountStatus.NoAccount, NSError())
  }
}

let loginManager = LoginManager.sharedInstance
let expectation = expectationWithDescription("iCloudStatus")

var isTestFinished = false
loginManager.iCloudStatusWithCompletionHandler { (status, error) -> Void in

  if (isTestFinished) {
    return
  }

  expectation.fulfill()
  XCTAssertTrue(status.isEqualToString("NoAccount"), "Status is Available")

}

waitForExpectationsWithTimeout(5, { error in
  isTestFinished = true
  XCTAssertNil(error, "Error")
})

But i am getting error while compiling the code

:0: error: cannot override 'init' which has been marked unavailable

What is best way I am using mock object to test my LoginManager class ?

Thanks


Solution

  • Currently there is no way to mock the CKContainer. The only solution would be to create a thin data access layer between your app and the CloudKit code and then mock that layer. But still you would not be able to unit test that thin layer itself.