Question

I'm reading "Test-Driven iOS Development" by Graham Lee to learn OCUnit. I think it's a few versions behind Xcode, but is still a useful resource.

In the book test objects are declared in a Class Extension like this:

@interface BROAnswerTests : XCTestCase {
    BROAnswer *answer;
    BROAnswer *otherAnswer;
}
@end

and then instantiated in - (void)setUp:

- (void)setUp
{
    [super setUp];
    answer = [[BROAnswer alloc] init];
    answer.text = @"The answer is 42";
    answer.person = [[BROPerson alloc] initWithName:@"Graham Lee" avatarLocation:
                     @"http://example.com/avatar.png"];
    answer.score = 42;
}

Would it be safe to use a @property in the Class Extension instead?

Was it helpful?

Solution

This is matter of personal choice.

If you use properties and you always refer to them with self.something or [self something], distinguishing between instance and local variables may be easier...

Personally I use ivars with the (hated by some people) "_" prefix.

In the only case where it could make a difference is when declaring properties with copy. In your particular case(a test class) you normally use properties to grab your object under test and/or its dependencies(mocked or not), so using copy in those scenarios is rare I think.

Use what you think is better for you/your team.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top