【问题标题】:How to achieve asynchronous task dependency with XCTestCases?如何使用 XCTestCases 实现异步任务依赖?
【发布时间】:2018-02-16 10:40:09
【问题描述】:

我正在为异步操作编写一些测试用例,其中我有两个需要执行单元测试的操作。

假设我有一些登录 web 服务需要调用,并且响应应该调用另一个配置文件 web 服务。

是否可以在 iOS 中使用单元测试来测试上述场景?

提前谢谢你。

【问题讨论】:

    标签: swift xcode xctestcase


    【解决方案1】:

    是的,这实际上很简单,您可以使用expectations 等待任务完成。

    例子:

    // Create an expectation for a background download task.
    let expectation = XCTestExpectation(description: "Login and fetch profile")
    
    MyApiClient.shared.login(username: username, password: password) { auth in
    
        // check login was successful before continuing
        MyApiClient.shared.fetchUserProfile(userId: auth.userId) { profile in 
    
            XCTAssertNotNil(profile)
    
            // Fulfill the expectation to indicate that the background task has finished successfully.
            expectation.fulfill()
        }
    }
    
    // Wait until the expectation is fulfilled, with a timeout of 10 seconds.
    wait(for: [expectation], timeout: 10.0)
    

    【讨论】:

      【解决方案2】:

      查看 Apple 提供的有关测试异步操作的资源:@​​987654321@

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 2013-01-05
        • 1970-01-01
        • 2018-12-17
        • 1970-01-01
        • 2015-11-06
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        相关资源
        最近更新 更多