Monday, March 30, 2009

STAssertEquals doesn't work for NSString (or any object for that matter)

STAssertEquals looks like a useful convenience function when you first stumble upon it, but it's not. SenTestCase_Macros.h reveals its dirty secret in the comments:

Generates a failure when a1 is not equal to a2. This test is for C scalars, structs and unions.

So it's fine for ints and so on, but not a simple workaround for the cumbersome-looking string equality test [string isEqualTo:@"hi!"]. Unfortunately, it doesn't warn you that you're using inappropriate arguments; that realisation is a couple of minutes investigation away.


  1. I was having the same problem -- you need to use STAssertEqualObjects.

  2. For object you need to use STAssertEqualObjects and for the value you need to use STAssertEqual!

    NSString *string1 = @"abcd"