How to check private members in objective block tests?

Given this class:

@interface SampleClass : NSObject { NSMutableArray *_childs; } - (void)addChild:(ChildClass *)child; - (void)removeChild:(ChildClass *)child; @end 

How can I check when I add a child if the _childs array contains one object without adding a property to access it (because I do not want client code to access the _childs array)?

+4
source share
3 answers

Create @property for it in the class extension and use the property for all calls, the way you test and use the same code.

+6
source

I'm not sure I understood your question correctly, I analyze it as: When implementing addChild: how can I prevent objects from being _childs second time in _childs ?

There are two ways: if the order of your elements does not matter, you should simply use NSMutableSet instead of NSMutableArray . In this case, the set takes care of everything:

 - (void)addChild:(ChildClass *)child { [_childs addObject:child]; } 

If order is important, stick with NSMutableArray and do it like this:

 - (void)addChild:(ChildClass *)child { if ([_childs containsObject:child]) return; [_childs addObject:child]; } 
+1
source

Just create a childCount (int) method that returns an array counter.

0
source

All Articles