问题描述
我的单元测试之一是失败,并且有一个原因我没有预料到。看来对 isKindOfClass
的调用返回NO,但是当我调试和逐步通过时,似乎没有理由。
代码是:
if([self.detailItem isKindOfClass:[MovieInfo class]]){
[self configureViewForMovie];
}
我走过代码并执行了:
po self.detailItem
显示:
(id)$ 1 = 0x0ea8f390< MovieInfo:0xea8f390>
所以,我错了什么,为什么if语句在这种情况下返回false?
编辑:
这里是DetailItem的设置器:
- (void)setDetailItem:(id)newDetailItem
{
if(_detailItem!= newDetailItem){
NSLog(@%@ [newDetailItem class]);
_detailItem = newDetailItem;
//更新视图。
[self configureView];
}
if(self.masterPopoverController!= nil){
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
这是来自主细节模板的模板代码。 p>
单元测试在setUp中创建MovieInfo:
movie = [[ MovieInfo alloc] initWithMovieName:@MoviemovieID:1];
并在测试中设置
controller.detailItem = movie;
此外,我在 setDetailItem
:
NSParameterAssert([newDetailItem isKindOfClass:[MovieInfo class]] || [newDetailItem isKindOfClass:[PersonInfo class]] || newDetailItem == nil);
此断言也失败。
我在断言调用之上放了两个日志语句:
NSLog(@%@,[newDetailItem class]) ;
NSLog(@%@,newDetailItem);
其中显示:
2012-08-28 08:31:37.574 Popcorn [8006:c07] MovieInfo
2012-08-28 08:31:38.253 Popcorn [8006:c07]< MovieInfo:0x6daac50&
更多编辑:
isKindOfClass
在单元测试中设置之前检查一次。
if([movie isKindOfClass:[MovieInfo class]]){
NSLog(@Yep); //这传递并打印出
}
controller.detailItem = movie; //调用setter并失败。
这是由于测试类DetailViewController不在测试的目标。我会希望这表现在一个不同的方式(链接器错误或东西),但显然,它只是导致奇怪的行为。将DetailViewController添加到测试目标中解决了问题。
One of my unit tests is failing and for a reason I didn't anticipate. It seems a call to isKindOfClass
is returning NO, but when I debug and step through, there seems to be no reason it would.
The code is:
if ([self.detailItem isKindOfClass:[MovieInfo class]]) {
[self configureViewForMovie];
}
I stepped through the code and did:
po self.detailItem
which displays:
(id) $1 = 0x0ea8f390 <MovieInfo: 0xea8f390>
So, what am I missing, why would the if statement return false in this case?
EDIT:
Here is the setter for the DetailItem:
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
NSLog(@"%@", [newDetailItem class]);
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
It's template code from a Master Detail template.
The unit test creates a MovieInfo in setUp:
movie = [[MovieInfo alloc] initWithMovieName:@"Movie" movieID:1];
and sets it in the test
controller.detailItem = movie;
Additionally, I added a parameter assertion in setDetailItem
:
NSParameterAssert([newDetailItem isKindOfClass:[MovieInfo class]] || [newDetailItem isKindOfClass:[PersonInfo class]] || newDetailItem == nil);
This assertion is failing as well.
I've put two log statements above the assertion call:
NSLog(@"%@", [newDetailItem class]);
NSLog(@"%@", newDetailItem);
which display:
2012-08-28 08:31:37.574 Popcorn[8006:c07] MovieInfo
2012-08-28 08:31:38.253 Popcorn[8006:c07] <MovieInfo: 0x6daac50>
ONE MORE EDIT:
I added the isKindOfClass
check before setting it in the unit test, that one passes.
if ([movie isKindOfClass:[MovieInfo class]]) {
NSLog(@"Yep"); //This passes and prints out
}
controller.detailItem = movie; //calls into the setter and fails.
This was due to the fact that the class under test "DetailViewController" was not in the test's target. I would have expected this to manifest itself in a different way (linker error or something), but apparently, it just causes odd behavior. Adding DetailViewController to the test target fixed the issues.
这篇关于isKindOfClass意外返回NO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!