Take a look at how the STAssert
macros in OCUnit (SenTestingKit, included with Xcode) are implemented.
In your own unit test bundle, you could implement a category on NSObject
to add methods like a hypothetical -shouldBeValid
which would then call the same pass/fail machinery that the STAssert
macros do now.
In case you're not intimately familiar with the C preprocessor...
You'll probably also have to use a #define
for your macros to pass through the right values for __FILE__
and __LINE__
when your BDD tests fail. For example, you might have to do something like this:
@interface NSObject (BehaviorDrivenDevelopment)
- (void)shouldBeValidInFile:(const char *)file line:(int)line;
@end
#define shouldBeValid shouldBeValidInFile:__FILE__ line:__LINE__
That way you would invoke it like this:
[[someObject methodUnderTest:argument] shouldBeValid];
The code the compiler sees will be this:
[[someObject methodUnderTest:argument] shouldBeValidInFile:__FILE__ line:__LINE__];
The __FILE__
and __LINE__
preprocessor macros will expand to the current file and line in your test source file.
This way, when you do have a failing test, it can pass appropriate information to SenTestingKit to send back to Xcode. The failure will show up correctly in the Build Results window, and clicking it will take you to the exact location of the failure in your tests.