新手撞蜂,一點(diǎn)筆記匹层。
//期望測(cè)試,可以在異步執(zhí)行結(jié)束后決定是否調(diào)用fullfill來(lái)通過(guò)測(cè)試。
- (void)testExpectation {
//Description會(huì)在test出錯(cuò)時(shí)打印在控制臺(tái),方便跟蹤稚补。
XCTestExpectation *expectation = [self expectationWithDescription:@"High Expectations"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
XCTAssertTrue(true);
[expectation fulfill];
});
//如果沒(méi)有fullfill或者超時(shí),都會(huì)失敗框喳。
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
//notification測(cè)試
- (void)testNotification {
[self expectationForNotification:@"TestNotification"
object:nil
handler:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:nil];
});
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
//謂詞測(cè)試
- (void)testPredicate {
NSString * str = @"haha";
NSPredicate *
predicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
//返回值true/false決定是否測(cè)試成功课幕,在這里寫(xiě)要測(cè)試的代碼厦坛。
NSString * str = evaluatedObject;
return [str isEqualToString:@"haha"];
}];
//這里的str參數(shù),可以在上面的block中讀取(evaluatedObject)乍惊。
[self expectationForPredicate:predicate
evaluatedWithObject:str
handler:nil];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
//KVO測(cè)試
- (void)testKVO {
UILabel * label = [[UILabel alloc] init];
label.text = @"hello";
//最后一個(gè)參數(shù)(@"nihao")為期望的kvo結(jié)果值杜秸,如果為空,則在值第一次變化時(shí)調(diào)用fullfill來(lái)通過(guò)測(cè)試润绎。
[self keyValueObservingExpectationForObject:label keyPath:@"text" expectedValue:@"nihao"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
label.text = @"nihao";
});
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}```