I need to test an instance variable in Objective C.
I have this snippet in myViewController.m
@implementation myViewController
{
NSView *myView;
}
I need to add a test in myViewControllerTest.mm
that tests a case when myView.window
happens to be nil
-(void) myTest
{
// 1. assert condition 1 is true
// 2. mock myView.window and set it to nil OR set myView.window directly to nil
// 3. assert condition 1 is false
}
I'd like to know the best approach to achieve step 2 in the test. Approaches I tried:
- (void)setMyView:(NSView*)myView
just for the unit test purpose and use that in myTest.
I have a feeling that this is not a good way.myView
as a property and put it in myViewController.h
so that I'll have a setter without explicitly defining one.Please suggest what the best approach is and what are the pros/cons of them. Thanks !
Edit : Answer I ended up using in my test file.
[myViewController setValue:nil forKeyPath:@"myViewControl.window"];
You can add setter only for testing in your test file myViewControllerTest.mm
This has no effect to your production code.
like below using Extension
// Write this in test file.
@interface MyViewController(Test)
- (void)setMyView:(NSView *)myView;
@end
or just use valueForKey:
to get private field.
-(void) myTest
{
id myViewController = ...;
NSView *myView = [myViewController valueForKey:@"myView"];
myView.window = nil;
// ...
}