I have following structure of objects:
Animal, Dog and Cat. As You expect Dog and Cat are inherited from Animal.
And I've a farm class:
@implementation AnimalFarm
-(Animal*) createAnimal:(AnimalType)type{
switch (type) {
case CAT:
return [Cat new];
case DOG:
return [Dog new];
default:
return [Animal new];
}
}
@end
and I tried to unit test:
AnimalFarm *farm = [AnimalFarm new];
Animal *dog = [farm createAnimal:DOG];
Animal *cat = [farm createAnimal:CAT];
STAssertTrue([cat isMemberOfClass:[Cat class]],@"cat is not a cat!");
STAssertTrue([dog isMemberOfClass:[Dog class]],@"Dog is not a dog!");
STAssertTrue([cat isKindOfClass:[Animal class]],@"Cat is not an animal!");
STAssertTrue([dog isKindOfClass:[Animal class]],@"Cat is not an animal!");
Implementation of classes:
@interface Cat : Animal {
}
@end
@implementation Cat
-(NSString*) say{
return @"miau";
}
@end
Implementation of dog is similar.
but isKindOfClass either isMemberOfClass didn't work as I expected....
Am I mising something?