How to check private members in objective C unit tests?
Considering this class :
@interface SampleClass : NSObject {
NSMutableArray *_childs;
}
- (void)addChild:(ChildClass *)child;
- (void)removeChild:(ChildClass *)child;
@end
How can i t开发者_JS百科est when i add a child if the _childs array contains one object without adding a property to access it (because i don't want allow client code to access the _childs array) ?
Create a @property for it in the class extension and use the property for all accesses, that way you are testing and using the same code.
I'm not sure I correctly understand your question, I parse it as: While implementing addChild:, how do I prevent to insert objects a second time into _childs ?
There are two ways: if the order of your elements doesn't matter then you should simply use a NSMutableSet instead of an NSMutableArray. In this case the set takes care of everything:
- (void)addChild:(ChildClass *)child
{
[_childs addObject:child];
}
If order is important, stick with the NSMutableArray and do it like this:
- (void)addChild:(ChildClass *)child
{
if ([_childs containsObject:child]) return;
[_childs addObject:child];
}
Just make a -(int)childCount method that returns the count of the array.
加载中,请稍侯......
精彩评论