0

Can someone please tell me that this assert could be optimized? Unit testing in iOS is driving me up the wall. A simple assertion on the count of an NSArray should not be this verbose:

GHAssertEquals([[NSNumber numberWithInt:[caseArray count]] intValue], 
                             [[NSNumber numberWithInt:627] intValue], 
                                              @"array count equals");

EDIT: Suggested line below

GHAssertEquals([caseArray count], 627, @"array count equals");

produces this output:

Reason: Type mismatch -- array count equals

0   CoreFoundation                      0x01cfd02e __exceptionPreprocess + 206
1   libobjc.A.dylib                     0x0113ae7e objc_exception_throw + 44
2   CoreFoundation                      0x01d85fb1 -[NSException raise] + 17
3   Tests                               0x00027711 -[GHTestCase failWithException:] + 33
4   Tests                               0x0001a0ed -[CaseTest testGetCaseArrayFromJSONArray] + 3293
5   libobjc.A.dylib                     0x0114e663 -[NSObject performSelector:] + 62
6   Tests                               0x00022e19 +[GHTesting runTestWithTarget:selector:exception:interval:reraiseExceptions:] + 450
7   Tests                               0x0001ea90 -[GHTest run:] + 275
8   Tests                               0x000211ea -[GHTestGroup _run:] + 696
9   Tests                               0x00021513 -[GHTestGroup run:] + 130
10  Tests                               0x000211ea -[GHTestGroup _run:] + 696
11  Tests                               0x00021513 -[GHTestGroup run:] + 130
12  Tests                               0x000239a2 -[GHTestRunner runTests] + 257
13  Tests                               0x00023b12 -[GHTestRunner _runInBackground] + 79
14  Foundation                          0x00b85805 -[NSThread main] + 76
15  Foundation                          0x00b85764 __NSThread__main__ + 1304
16  libsystem_c.dylib                   0x983c7557 _pthread_start + 344
17  libsystem_c.dylib                   0x983b1cee thread_start + 34

FAIL (0.033s)
mccrackend
  • 137
  • 8

2 Answers2

5

"Type mismatch" is because GHAssertEquals requires that both arguments be the same type. [caseArray count] returns an unsigned integer. This doesn't match 627 which is a signed integer. So instead, compare against an unsigned 627, namely 627U:

GHAssertEquals([caseArray count], 627U, @"array count equals");

Alternatively, you could use OCHamcrest to say

assertThat(caseArray, hasCountOf(627));

(OCHamcrest is compatible with GHUnit.)

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
0

I don't know GHAsserEquals, but your expression is equivalent to:

GHAssertEquals([caseArray count], 627, @"array count equals");
danh
  • 62,181
  • 10
  • 95
  • 136
  • One would think that your answer would be sufficient, but generates output like so: Reason: Type mismatch -- array count equals 0 CoreFoundation 0x01cfc02e __exceptionPreprocess + 206 1 libobjc.A.dylib 0x01139e7e objc_exception_throw + 44 2 CoreFoundation 0x01d84fb1 -[NSException raise] + 17 3 Tests 0x00026bc1 -[GHTestCase failWithException:] + 33 4 Tests 0x0001a34f -[CaseTest testGetCaseArrayFromJSONArray] + 2831 – mccrackend Feb 01 '13 at 02:57
  • 2
    Not sure I understand, but I'm confident that no matter what GHAssertEquals does, your code and my code are passing _exactly_ the same thing to it. Maybe it requires objects? You could try leaving off the intValue calls in your code. – danh Feb 01 '13 at 03:02
  • 1
    @mccrackend, You should post that in question. What your code does and the code above are the same. – iDev Feb 01 '13 at 03:04
  • When I tried this suggestion, GHUnit fails with this Reason: Type mismatch -- array count equals. I also agree that your code should be the exact same - I'm getting a bit frustrated using GHUnit and was hoping I was just way off here in how I'm using it. – mccrackend Feb 01 '13 at 03:05
  • Thanks for all the replies - I tried removing the intValue calls and got a failure Reason: '<101b0308>' should be equal to '<20c30308>'. array count equals. – mccrackend Feb 01 '13 at 03:11
  • @mccrackend, I think Jon Reid has already given a perfect answer. Check it. – iDev Feb 01 '13 at 03:16