菜鸟教程小白 发表于 2022-12-11 18:51:25

ios - 在 XCode 中开始下一个单元测试之前等待 XCTestExpectation


                                            <p><p>我有许多异步单元测试,它们使用期望可以正常工作。但是,当我运行套装中的所有测试时,它们不会等待彼此完成 - 当下一个测试开始运行时,异步回调仍处于挂起状态。我想要的是每个测试在运行之前等待上一个测试中的期望。这些测试使用共享数据库,因此让它们重叠会导致烦人的额外复杂性,并且在作为套件运行时会导致测试失败。</p>

<pre><code>- (void)testSignIn {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.

XCTestExpectation *expectation =
;

[_userManager signInWithUsername:kUserEmail andPassword:kUserPassword
                         success:^{
                           XCTAssertNotNil();

                           XCTAssertNotNil(.plan);
                           XCTAssertTrue(.plan.liveStream == TRUE);

                           ;

                         } failure:^(EDApiError *apiError) {
                           XCTAssertTrue(FALSE); // Should not fail
                           ;

                         }];

[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
    if (error) {
      NSLog(@&#34;Timeout Error: %@&#34;, error);
    }
}];
</code></pre>

<p>}</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用 <a href="https://developer.apple.com/documentation/xctest/xctwaiter" rel="noreferrer noopener nofollow">XCTWaiter</a>或 <a href="https://developer.apple.com/documentation/xctest/xctestcase/1500748-waitforexpectations" rel="noreferrer noopener nofollow">waitForExpectations(timeout:handler:)</a>推迟每个测试的完成,直到达到预期为止。</p>

<p>这篇博文解释了您在编写异步测试时可能遇到的一些更复杂的陷阱以及如何防止它们:<a href="https://jeremywsherman.com/blog/2016/03/19/xctestexpectation-gotchas/" rel="noreferrer noopener nofollow">https://jeremywsherman.com/blog/2016/03/19/xctestexpectation-gotchas/</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 XCode 中开始下一个单元测试之前等待 XCTestExpectation,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46497158/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46497158/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 XCode 中开始下一个单元测试之前等待 XCTestExpectation