1

I'm trying to access a private class variable in my unit test:

Class
 - private variable abc;

unit test
category/extension above the unittest m file content
 @property (...) variable abc;

but within the test, I always get a unrecognized selector error... Is there any trick making private variables visible/testable?

Sorry, found nothing using google ;)

Greetings, matthias

matttrakker
  • 204
  • 3
  • 15

1 Answers1

2

try [obj valueForKey:@"_ivar"]

you can also make a category to that class and you can access any private variables during the method

@implementation MyClass (UnitTestAddition)

- (id)getPrivateVariable {
    return _ivar;
}

@end
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • excelent, obj valueforkey is working - do you know why obj._ivar does not? Thanks!!! – matttrakker Jan 10 '13 at 12:47
  • to do or obj.ivar, the class must implemented `[ivar]` either in the implementation or synthesized by the compiler. otherwise it will be unrecognized selector exception. however, you can implement the accessor in the category – Bryan Chen Jan 10 '13 at 12:57