使用XCTest,如何将{期望 - >的多个离散序列链接在一​​起?等待}?

时间:2015-03-19 16:44:42

标签: xctest xctestexpectation

XCTest waitForExpectationsWithTimeout:handler:的文档说明了

  

只有一个-waitForExpectationsWithTimeout:handler:可以在任何给定时间激活,但是{expected - >的多个离散序列等待}可以链接在一起。

但是,我不知道如何实现这一点,也找不到任何例子。我正在研究一个首先需要找到所有可用串口的类,选择正确的端口,然后连接到连接到该端口的设备。所以,我正在处理至少两个期望,XCTestExpectation * expectationAllAvailablePorts和* expectationConnectedToDevice。我如何将这两个链接起来?

5 个答案:

答案 0 :(得分:7)

我执行以下操作并且有效。

expectation = [self expectationWithDescription:@"Testing Async Method Works!"];

[AsynClass method:parameter callbackFunction:^(BOOL callbackStatus, NSMutableArray* array) {
    [expectation fulfil];
    // whatever
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
    if (error) {
        XCTFail(@"Expectation Failed with error: %@", error);
    }
    NSLog(@"expectation wait until handler finished ");
}];

// do it again

expectation = [self expectationWithDescription:@"Testing Async Method Works!"];

[CallBackClass method:parameter callbackFunction:^(BOOL callbackStatus, NSMutableArray* array) {
    [expectation fulfil];
    // whatever
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
    if (error) {
        XCTFail(@"Expectation Failed with error: %@", error);
    }
    NSLog(@"expectation wait until handler finished ");
}];

答案 1 :(得分:3)

将我的期望分配给弱变量对我有用。

答案 2 :(得分:2)

迅速

let expectation1 = //your definition
let expectation2 = //your definition

let result = XCTWaiter().wait(for: [expectation1, expectation2], timeout: 10, enforceOrder: true)

if result == .completed {
    //all expectations completed in order
} 

答案 3 :(得分:0)

这似乎也适用于Swift 3.0。

  #include<stdio.h>

int main()
{
  int matrix1[4][4];
  int matrix2[4][4];
  int matrix3[4][4];
  int a;
  int b;
  int c;
  int sum;
  int multi = 0;

  //first matrix
  for(a=0; a<4; a++)
  {
    for(b=0; b<4; b++)
    {
      scanf("%d", &matrix1[a][b]);
    }
  }
  //second matrix
  for(a=0; a<4; a++)
  {
    for(b=0; b<4; b++)
    {
      scanf("%d", &matrix2[a][b]);
    }
  }
//Multiplication:
  for(a=0; a<=3; a++)
  {
    for(b=0; b<=3; b++)
    {
      sum=0;
      for(c=0; c<=3; c++)
      {
        while(matrix2[c][b]>0)
        {
          multi += matrix1[a][c];
          matrix2[c][b]--;
        }
      }
      sum = sum+multi;
      matrix3[a][b]=sum;
    }
  }

  //result;
  for(a=0; a<4; a++)
  {
    for(b=0; b<4; b++)
    {
      printf(" %d ", matrix3[a][b]);
    }
    printf("\n");
  }
  return 0;
}

答案 4 :(得分:0)

在扩展XCTestCase的类中,您可以像这样使用wait(for:timeout:)

let expectation1 = self.expectation(description: "expectation 1")
let expectation2 = self.expectation(description: "expectation 2")
let expectation3 = self.expectation(description: "expectation 3")
let expectation4 = self.expectation(description: "expectation 4")

// ...
// Do some asyc stuff, call expectation.fulfill() on each of the above expectations.
// ...

wait(for:[expectation1,expectation2,expectation3,expectation4], timeout: 8)