【问题标题】:XCTestCase setUp once with async operationXCTestCase 使用异步操作设置一次
【发布时间】:2015-07-01 13:58:07
【问题描述】:

我正在尝试为使用 Web 服务 API 的 iOS 客户端编写一些单元测试。我想使用实际的服务而不是模拟数据。

在所有测试可以运行之前,我需要获得一个令牌来验证用户的身份。 我正在尝试使用类 setUp 方法来做到这一点 - 但我不确定在继续其余测试之前如何等待令牌到达(因为所有网络调用都是异步的)。

【问题讨论】:

    标签: objective-c xcode swift unit-testing xctest


    【解决方案1】:

    您可以创建一个等待令牌的函数,使用一个虚拟(内部没有验证)assync 测试,如下所示:

     func waitforToken(){
        let dummyExpectation = expectationWithDescription("Expect to get my token")
        ... call web service
    
        waitForExpectationsWithTimeout(20, handler: { error in
             XCTAssertNil(error, "Error")
        })
    }
    //on the web service Callback fulfill the expectation
    func webServiceCallback(expectation:expectationWithDescription, ...)  {
       ...
       expectation.fulfill()
    }
    

    然后在每次测试开始时调用它。如果你在setUp 内部调用可能不会起作用,但我可能是错的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多