我在我的应用程序中使用 CloudKit,并尝试模拟 CKContainer 来测试我的管理器。这是我尝试过的:
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")
})
但是我在编译代码时遇到错误
:0: error: cannot override 'init' which has been marked unavailable
使用模拟对象测试我的 LoginManager 类的最佳方法是什么?
谢谢
最佳答案
目前还没有办法模拟 CKContainer。唯一的解决方案是在您的应用程序和 CloudKit 代码之间创建一个薄数据访问层,然后模拟该层。但您仍然无法对那个薄层本身进行单元测试。
关于ios - 模拟 CKContainer 用于单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28772988/