我的方法返回一個NSNumber *,我想單元測試這個方法。由於我的實際返回值是NSNumber *,我創建了一個NSNumber *的新期望值,但失敗了。下面是代碼:如何使用xcttest測試對象相等?
NSNumber *cRating = [movie getRating:ratingDictionary ratingType:criticRating];
XCTAssertEqualObjects(cRating, [[NSNumber alloc]initWithInt:70], @"");
的錯誤是:
[SFModelTest testGetCriticRatingMethod] failed: ((cRating) equal to ([[NSNumber alloc]initWithInt:70])) failed: ("70") is not equal to ("70")
因爲它是說「70」不等於「70」,我猜測它與ALLOC初始化做。一些指針的東西不相等。有人可以幫忙嗎?謝謝。
發表評論編輯:添加getRating方法
- (NSNumber *)getRating:(NSDictionary *)movieDic ratingType:(enum RatingsEnum) rating{
NSNumber *result = 0;
NSNumber *ratingNum = 0;
switch (rating) {
case userRating:
{
ratingNum = [movieDic objectForKey:@"audience_score"];
break;
}
case criticRating:
{
ratingNum = [movieDic objectForKey:@"critics_score"];
break;
}
default:
break;
}
if(ratingNum && ratingNum > 0)
{
result = ratingNum;
}
return result;
}
該方法返回的NSNumber。我的測試是:
NSNumber *cRating = [movie getRating:ratingDictionary ratingType:criticRating];
XCTAssertEqualObjects(cRating, [[NSNumber alloc]initWithInt:70], @"");
當我做一個NSLOG類時,它返回__NSCFConstantString。我現在很困惑。
在'getRating:ratingType:'方法中,你如何創建'NSNumber'? – nhgrif
NSNumber * result = 0;然後我在方法結尾返回結果。 –
嘗試記錄'cRating'的類。即'NSLog(@「%@」,[cRating class])'確定它不是'NSString'或其他東西 –