I've just started writing tests for my iOS app, I'm using Xcode4 and OCUnit. I'm now writing a test for a piece of code that should throw an exception if a static variable has already been assigned to, like this:
NSAssert(controllerInstance == nil, @"anoth开发者_高级运维er controller is already in use!");
In my test I use STAssertThrows
to check that the above code throws an error if the instance already exists. The problem is that this test fails with:
*** Assertion failure in (reference to row with NSAssert)
So the test is failing even though the behavior is exactly the one I want. But shouldn't STAssertThrows
catch this? Maybe I shouldn't use NSAssert together with STAssertThrows
in this way?
I think the assertion handler prints that message before raising the exception. If that's not it, STAssertThrows()
probably ignores assertion failures.
In any case, why are you testing that NSAssert works? Do you not trust the Cocoa framework?
Edit
Just noticed the second part to your question. The answer is no, you should not be using STAssertThrows and NSAssert together in this way. NSAssert()
is there to pick up cases of programming error where an assumption of the programmer is false. By definition, a piece of code that asserts has failed its test case.
精彩评论