【发布时间】:2015-02-06 11:44:22
【问题描述】:
我正在用 Swifts 编写单元测试,并测试一个独特的工作流程。
-
在 methodA() 中,我使用异步方法错误地加载了一个对象(比如使用不正确的凭据)。也开启期待
func methodA(withCred credential: NSURLCredential) { var objA = ObjectA() // Set objA.a, objA.b, objA.c, objA.credential = credential //Incorrect credential First time, Correct Credential second time objA.delegate = self expectation = expectationWithDescription(“Aync”) objA.callAsyncMethod() //This fires successDelegate() or failureDelegate()} -
当 FailureDelegate() 被触发时,我重新加载对象,这次是正确的。为此,我需要再次调用 MethodA() (这样我就可以重用那里的所有其他东西)。
func failureDelegate(error: NSError!) { XCTAssertTrue(error.localizedDescription == “Invalid Credentials“) //Now that I’ve verified correct error is returned, I need to reload objA methodA(withCred:correctCredential) } func successDelegate(obj : ObjectA) { XCTAssert(“Object is loaded”) expectation.fulfill() }
3.这在方法A中再次启动了相同的期望,并导致以下错误:
API 违规 - 在等待模式下创建预期。
我了解 swift 不允许这样做。是否有解决方法或更好的方法来测试这些使用 XCTest 与 Swift 循环的异步方法?
谢谢!
【问题讨论】:
标签: ios unit-testing swift xctest