我正在尝试用XCTAssert编写单元测试。我有一个NSSet,我想测试这个集合是否包含任何对象。
我向以下机构查询:
XCTAssertTrue((mySet.count == 0), @"mySet should not be empty");考试总是通过的。在我的测试中,NSSet是空的。当我插入if-语句并请求if (mySet.count == 0)时,这是真的,因此它们不是NSSet中的任何元素。
为什么断言没有被打破?或者:如何检查NSSet或NSArray是否与XCTAssert为空?
发布于 2014-02-12 16:28:46
函数的格式是
XCTAssertTrue( <some condition>, @"Some string that gets printed to the console if the test fails" )如果某个条件的计算结果为true,则测试将通过;如果为false,则失败。示例:
// 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应该始终保存数据。当计数为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 -语句并请求if (mySet.count == 0)时,这是真的,因此它们不是NSSet中的元素。
如果您的集合为空,则XCTAssertTrue( mySet.count == 0, @"" )通过,因为mySet中没有项。
https://stackoverflow.com/questions/21711770
复制相似问题