OCUnit on private properties

133 views Asked by At

I am new to test driven development and have a basic question. How do I access private properties for unit testing? There are plenty of answers on how you access methods but I couldn't find much on properties. Here is what I've got now for my class CDTest. Excluding some of the imported headers for readability purposes.

CDTest.h

@interface CDTest : NSObject

@end

CDTest.m

#import "CDTest.h"
#import "CDTest+Protected.h"

@interface CDTest()

@property (strong, nonatomic) NSManagedObjectContext *context;

@end

@implementation CDTest

- (void)setup
{
    //Sets up the context
}

@end

CDTest+Protected.h

@interface CDTest()

- (void)setup;

@end

Unit test .m file

#import "CDTest.h"
#import "CDTest+Protected.h"

@implementation CDTestTests
{
    CDTest *cdTest;
}

- (void)setUp
{
    [super setUp];
    cdTest = [CDTest new];
}

- (void)testSetup
{
    [cdTest setup];
    //Now I need access to the properties in cdTest to validate them
}

@end
1

There are 1 answers

1
JeremyP On BEST ANSWER

In Objective-C properties are methods (each property is a getter and a setter if it is read/write).

Just declare your property in your protected category, which should be declared something like this

@interface CDTest(Protected)

- (void)setup;
@property (strong, nonatomic) NSManagedObjectContext *context;

@end