菜鸟教程小白 发表于 2022-12-11 16:44:45

ios - 使用 XCTestExpectation 对异步函数进行单元测试,但它不会等待我设置的秒数


                                            <p><p>我有一个继承 <code>NSThread</code> 的 <code>MyService</code> 类:</p>

<p>标题:</p>

<pre><code>@interface MyService : NSThread {
-(void) startMe;
-(void) doTask;
...
}
</code></pre>

<p>实现:</p>

<pre><code>@implementation MyService
-(void)startMe {
   ;
}
-(void) doTask {
    ;
}

-(void) checkData {
    ...
    // NOTE: dataChecked is an instance variable.
    dataChecked = YES;
}
@end
</code></pre>

<p>我想对上面的 <code>-(void)doTask</code> 进行单元测试并验证 <code>-(void)checkData</code> 是否真的被调用了。我用 <a href="https://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/" rel="noreferrer noopener nofollow">OCMock library</a>部分模拟 <code>MyService</code>。 </p>

<p>灵感来自 <a href="https://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/" rel="noreferrer noopener nofollow">this tutorial (use XCTestExpectation)</a> ,我尝试了以下方法:</p>

<pre><code>       -(void) testCheckData {
          // partial mock MyService
          id myService = ];
          ;

          // function to test
          ;

          // I setup expectation
          XCTestExpectation *expectation = ;

         // run assertion asynchronisely
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            XCTAssertTrue();
            // expectation fulfill
            ;
      });

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

<p>但是,当我运行测试时,<a href="http://masilotti.com/xctest-documentation/Classes/XCTestCase.html#//api/name/waitForExpectationsWithTimeout:handler:" rel="noreferrer noopener nofollow">waitForExpectationsWithTimeout:handler:</a>不起作用,我的意思是它不会等待 5 秒,断言部分在被测函数被调用后立即运行。为什么它不等待 5 秒?</p>

<p><strong>====== 更新 ======</strong></p>

<p>我也试过不使用异步 block :</p>

<pre><code>-(void) testCheckData {
      // partial mock MyService
      id myService = ];
      ;

      // function to test
      ;

      // I setup expectation
      XCTestExpectation *expectation = ;

       // run assertion
       XCTAssertTrue();
       // expectation fulfill
       ;

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

<p>但是我还是遇到同样的问题,没有等待5秒,测试立即返回,为什么?</p>

<p><strong>===== AND =====</strong></p>

<p>如果我们忽略上面的更新并返回查看我的原始代码<strong>使用异步 block </strong>,我可能会感到很痛苦。我认为 <code>waitForExpectations:5</code> 应该等待我不需要使用 <code>while</code> 循环,为什么我认为这种方式是因为 <a href="https://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/" rel="noreferrer noopener nofollow">tutorial</a> .</p>

<p>如果我们查看那个教程,它首先显示了<strong>使用while循环的旧风格</strong>等待,然后,它变成了<strong>不使用任何while循环的期望风格</strong >,它所做的是设置期望->开始工作(在其工作的完成 block 中断言),它还有 <code>waitForExpectations:</code> 代码,看起来和我的代码完全一样 <strong>with异步 block </strong>。我想了解为什么我的原始代码看起来与教程相同但不起作用。我错过了什么吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>一旦您开始<strong>waitForExpectations</strong>,您的 dispatch_async 就会有机会运行。它会断言您的数据已经过检查,然后——无论你的数据是否已经过检查——它都将期望标记为已实现。一旦完成,我们不再需要等待,我们可以完成。</p>

<p>这可能不是您想要做的,但这就是代码所说的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 XCTestExpectation 对异步函数进行单元测试,但它不会等待我设置的秒数,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37845892/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37845892/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 XCTestExpectation 对异步函数进行单元测试,但它不会等待我设置的秒数