Dave Durbin
Dave Durbin

Reputation: 3632

Using SenTest to test an assertion

I have code in method which asserts( ) that a parameter falls within a given range. I'd like to test illegal parameters using a SenTest test case.

My first assumption was that I should use STAssertThrows( ... ) however this reports no exception is thrown when the assert fails. Can I catch assert() fails with an STAssert... macro?

[updated to provide an example]

In class Foo.m

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

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

In class TestFoo.m

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

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

What XXX and YYY can I use to test the failure or otherwise of the assertion in method bar: ?

Upvotes: 1

Views: 546

Answers (2)

titaniumdecoy
titaniumdecoy

Reputation: 19251

If you use NSAssert (or NSAssert1, NSAssert2, etc.) instead of assert, you can catch an NSInternalInconsistencyException.

Upvotes: 2

Sulthan
Sulthan

Reputation: 130102

You can't catch assert because it is not an obj-c exception. To fix this, just declare your own macro MY_ASSERT(condition) which will throw an exception if the condition is not met and use it instead of standard assert.

Upvotes: 0

Related Questions