2

I am new to Objective-C and I am trying to start right away with TestDrivenDevelopment, since I find it really assuring, when at least the tests do pass.

Before that I made some Tutorials in Java where I got a little understanding for TDD. Brett Schucherts Video-Tutorials where he goes step by step through coding a full RPNCalculator is a gold mine to learn the stuff by watching him in action.

To reduce code duplication there is for instance this nice thing where you do:

@Before
public void init() {
    /* Stuff that which will be set up before the call of each test method/*
}

which is then called before each test you have in your TestClass in Java.

This I want to realize in Objective-C + Xcode. I should mention, that I am using Xcode 4.3 (latest version) and that I am using the built in TestFramework.

The only thing I found in the web that came near what I am looking for was this answer on SO.

Unfortunately I am not able to reproduce the described way of doing things. A minimal runnable example and/or a more detailed explanation for a Newcomer would would be awesome and well appreciated!

By the way, sorry for the bad english. Still learning the language. :-)

Edit:

Here is a minimal example which does not work. Perhaps someone can tell me what is wrong. Xcode seems not to be able to recognize board inside the body of the test methods.

#import <SenTestingKit/SenTestingKit.h>
@interface ABoardShould : SenTestCase
@end

#import "ABoardShould.h"
@implementation ABoardShould

- (void)setUp
{
    [super setUp];

    int rowCount = 6;
    int columnCount = 7;
    Board *board = [[Board alloc] initWithShapeRowCount:rowCount andColumnCount:columnCount];
}

- (void)tearDown
{
    // Tear-down code here.

    [super tearDown];
}

- (void)testHaveItsShapeSetWhenInitialised {
    STAssertEquals([board rowCount], rowCount, @"");
    STAssertEquals([board columnCount], columnCount, @"");
}

- (void)testHaveTheDimensionsOfItsBoardMatchTheGivenShape {        
    NSMutableArray *expectedFields = [[NSMutableArray alloc] initWithCapacity:columnCount*rowCount];
    for(int i=0; i < (rowCount*columnCount); i++) [expectedFields addObject: [NSNumber numberWithInt: 0]];

    STAssertEquals([expectedFields count], [[board fields] count], @"");
}
Community
  • 1
  • 1
Aufwind
  • 25,310
  • 38
  • 109
  • 154

1 Answers1

3

The setup and teardown methods of your test case class must be called setUp and tearDown. This is described in Xcode Unit Testing Guide: Writing Test Case Methods.

In your example, board is a local variable in the setUp method. You need to make it an instance variable.

Jenn
  • 3,143
  • 1
  • 24
  • 28
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks for the link. I posted an example above that should explain where my problems are, since I still didn't manage to get it to work. – Aufwind Apr 29 '12 at 03:46