該函數的格式是
XCTAssertTrue(<some condition>, @"Some string that gets printed to the console if the test fails")
測試通過如果一些條件評估爲真,並且如果假失敗。例如:
// create empty set
NSSet *mySet = [[NSSet alloc] init];
// this test passes because the set is empty
XCTAssertTrue([mySet count] == 0, @"Set should be empty");
// Set with three items
NSSet *setTwo = [[NSSet alloc] initWithArray:@[ @"1", @"2", @"3" ]];
// passes test because there are three items
XCTAssertTrue([setTwo count] == 3, @"We should have three items");
// failing test
XCTAssertTrue([setTwo count] == 0, @"This gets printed to the console");
回到你的問題:
我不想讓測試失敗時的NSSet是空的。所以NSSet 應該始終保存數據。當count爲0時 - >出現錯誤。
你想測試一些項目已被添加到mySet
。有兩個測試可以使用:
XCTAssertTrue([mySet count] > 0, @"Should have at least one item");
// or
XCTAssertFalse([mySet count] == 0, @"mySet count is actually %d", [mySet count]);
另外:
在我的測試用例中的NSSet是空的。當我插入一個if語句和 要求,如果(mySet.count == 0),它是真實的 - 所以他們在 沒有元素的NSSet
如果設置爲空,XCTAssertTrue(mySet.count == 0, @"")
通行證因爲沒有物品在mySet
。
你是如何創建該集合,並應該在這一點上是空的?你傳入的字符串與實際測試相矛盾。 –
NSSet是一個來自NSManagedObject的關係。當NSSet爲空時,我想讓測試失敗。所以NSSet應該始終保存數據。當count爲0時 - >出現錯誤。 – Sebastian