简体   繁体   中英

Unit Test Fails — Why?

I have the following test in my unit test suite:

STAssertEquals(0, [[newUnit itemsWithinBasketFrom:[NSDate dateYesterday] through:[NSDate dateTomorrow]] count],
               @"A unit with no items should return 0 when asked for items in place within a date range. (count=%i)",
               [[newUnit itemsWithinBasketFrom:[NSDate dateYesterday] through:[NSDate dateTomorrow]] count]);

And the output from the build console is: Type mismatch -- A unit with no items should return nil when asked for items in basket within a date range. (count=0) Type mismatch -- A unit with no items should return nil when asked for items in basket within a date range. (count=0) .

If count is 0, and I'm testing its equality against 0, then why do I get a type mismatch?

Thanks!

count returns NSUInteger which is unsigned int . You set the expected value as 0 which is int . Those types are different ones. Cast your 0 to NSUInteger like (NSUInteger)0 or use 0U .

IMO, the problem is not the count is 0 or not. What is the returned type of [[newUnit itemsInPlaceWithinDateRangeFrom:[NSDate dateYesterday] through:[NSDate dateTomorrow]] count] . Maybe your type is not correct (like double), then when you try to print it out, it appears to be 0. And because it can not compare int and double, it generates type mismatch. It is just my guess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM