私はiOS9 の期待を使用して非同期テストを使用するこの例を見ています。これは、期待を満たすことができる完了ブロックを持つネットワーク要求やその他の操作に適しています。
ただし、コールバック ブロックを使用しないクラスを単体テストしようとしています。これまでのところ、以下のコードが時々機能することがわかりましたが、正しいアプローチを使用しているかどうかはわかりません。
アプリがバックグラウンドで何かを実行している間、テスト ケースを遅らせるにはどうすればよいですか? この場合、実行セレクターを使用して何かをチェックし、タイムアウト前にそのバックグラウンド スレッドからの期待を満たすことはできますか?
-(void)checkFoundExpectedSubview:(MockViewController*)mockViewController
{
if(mockViewController.didFindExpectedSubview)
{
[self.expectation fulfill];
}
}
-(void)testErrorMessage
{
MockViewController* mockViewController = [MockViewController create];
[mockViewController expectSubviewWithClass:@"TSMessageView"];
NSString* title = @"Error!";
NSString* subtitle = @"This is a unit test of an error message";
[self.errorHandler reportErrorWithTitle:title message:subtitle];
//this checks and fulfills my expectation
[self performSelector:@selector(checkFoundExpectedSubview:) withObject:mockViewController afterDelay:2];
self.expectation = [self expectationWithDescription:@"Waiting for the error to be presented"];
[self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) {
XCTAssertNil(error);
}];
}