【发布时间】:2016-05-15 18:36:22
【问题描述】:
我正在尝试使用钥匙串进行基本的单元测试。我可以成功设置键的值('setString' 返回 true),但是,当我尝试检索值时,即使延迟 5 秒后,返回仍然为零:
class MyKeychainTest: XCTestCase {
func checkKeychain(timer: NSTimer) {
debugPrint("check keychain...")
let userInfo = timer.userInfo as! [String: AnyObject]
let expectation = userInfo["expectation"] as! XCTestExpectation
let res = KeychainWrapper.objectForKey("myKey")
debugPrint("got res: \(res)")
XCTAssertNotNil(res)
expectation.fulfill()
}
func testKeychain() {
let expectation = expectationWithDescription("gotKey")
let success = KeychainWrapper.setString("foo", forKey: "myKey")
debugPrint("set key?: \(success)")
NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(self.checkKeychain)
, userInfo: ["expectation": expectation ], repeats: false)
self.waitForExpectationsWithTimeout(10, handler: nil)
}
}
有什么可能导致这种情况的想法吗?
谢谢
【问题讨论】: