有人可以告诉我可以优化此断言吗? iOS中的单元测试使我望而却步。一个简单的关于NSArray计数的断言不应这么冗长:
GHAssertEquals([[NSNumber numberWithInt:[caseArray count]] intValue],
[[NSNumber numberWithInt:627] intValue],
@"array count equals");
编辑:下面的建议行
GHAssertEquals([caseArray count], 627, @"array count equals");
产生以下输出:
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)
最佳答案
“类型不匹配”是因为GHAssertEquals要求两个参数都为同一类型。 [caseArray count]
返回一个无符号整数。这与有符号整数627不匹配。因此,与未签名的627(即627U
)进行比较:
GHAssertEquals([caseArray count], 627U, @"array count equals");
或者,您可以使用OCHamcrest来说
assertThat(caseArray, hasCountOf(627));
(OCHamcrest与GHUnit兼容。)