3

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, @"another 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?

1 Answer 1

3

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.

1
  • 1
    Alright, point taken. Then I wont use NSAssert in this case as I would rather gather the assumptions about how the code should work in my test cases.
    – Cactuar
    Commented Jun 21, 2011 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.