Search code examples
objective-ctddprivate-membersgh-unit

GHUnit access private variable


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


Solution

  • 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