首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XCTAssert NSSet计数

XCTAssert NSSet计数
EN

Stack Overflow用户
提问于 2014-02-11 19:59:17
回答 1查看 3.2K关注 0票数 1

我正在尝试用XCTAssert编写单元测试。我有一个NSSet,我想测试这个集合是否包含任何对象。

我向以下机构查询:

代码语言:javascript
复制
XCTAssertTrue((mySet.count == 0), @"mySet should not be empty");

考试总是通过的。在我的测试中,NSSet是空的。当我插入if-语句并请求if (mySet.count == 0)时,这是真的,因此它们不是NSSet中的任何元素。

为什么断言没有被打破?或者:如何检查NSSet或NSArray是否与XCTAssert为空?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-12 16:28:46

函数的格式是

代码语言:javascript
复制
XCTAssertTrue( <some condition>, @"Some string that gets printed to the console if the test fails" )

如果某个条件的计算结果为true,则测试将通过;如果为false,则失败。示例:

代码语言:javascript
复制
// 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中的项。有两个测试可以使用:

代码语言:javascript
复制
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中没有项。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21711770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档