【问题标题】:Using SenTest to test an assertion使用 SenTest 测试断言
【发布时间】:2012-10-06 02:08:44
【问题描述】:

我在方法中有代码,它断言()参数落在给定范围内。我想使用 SenTest 测试用例测试非法参数。

我的第一个假设是我应该使用 STAssertThrows( ... ) 但是当断言失败时,这报告不会引发异常。我可以用 STAssert... 宏捕获 assert() 失败吗?

[更新以提供示例]

在 Foo.m 类中

@interface Foo : NSObject {
    NSUInteger count;
    NSUInteger max;
}
@end

@implementation Foo
-(void) bar:(char) c {
    assert( count < max );
    ...
}
@end

在 TestFoo.m 类中

@interface TestFoo : SenTestCase {
    Foo testFoo_;
}
@end

@implementation TestFoo
    -(void) testBar {
        STAssertXXX( YYY );
    }
@end

我可以使用什么 XXX 和 YYY 来测试方法栏中的断言是否失败:?

【问题讨论】:

  • 如果您向我们展示您尝试做出的断言示例,也许我们可以帮助您更轻松。

标签: objective-c unit-testing testing assert


【解决方案1】:

如果您使用NSAssert(或NSAssert1NSAssert2 等)而不是assert,则可以捕获NSInternalInconsistencyException

【讨论】:

    【解决方案2】:

    你无法捕捉到assert,因为它不是 obj-c 异常。要解决此问题,只需声明您自己的宏 MY_ASSERT(condition),如果条件不满足,它将抛出异常并使用它而不是标准的 assert

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2020-05-29
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多