views:

989

answers:

4
+3  Q: 

BDD in Objective-C

I have recently started to learn Objective-C and write my tests using OCUnit that comes bundled with Xcode.

I'm a long time Ruby programmer and I'm used to RSpec and Cucumber - nice BDD frameworks.

Is there a decent BDD framework to use in Objective-C? I'm missing my 'should's :)

A: 

There is nothing stopping you prefixing your test method with Should. I did that with NUnit in C#.

Michaël Larouche
What I'm after is specific syntax for verification. In Ruby it looks like:method_under_test(args).should be_valid
Bragi Ragnarson
The person asking the question is talking about Objective-C and OCUnit, which expects test methods themselves to begin with "test" - that's how it knows what methods are test methods, since Objective-C doesn't have annotations like C# and Java do.
Chris Hanson
+4  A: 

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.

Chris Hanson
+4  A: 

There's a relatively new project called uispec which was inspired by RSpec's testing DSL. The example spec looks like this:

#import "DescribeEmployeeAdmin.h"
#import "SpecHelper.h"

@implementation DescribeEmployeeAdmin

-(void)before {
  //login as default admin before each example
  [SpecHelper loginAsAdmin];
}

-(void)after {
  //logout after each example
  [SpecHelper logout];
}

-(void)itShouldHaveDefaultUsers {
  //Check that all default users are in list
  [[app.tableView.label text:@"Larry Stooge"] should].exist;
  [[app.tableView.label text:@"Curly Stooge"] should].exist;
  [[app.tableView.label text:@"Moe Stooge"] should].exist;
}

-(void)itShouldAddAUser {
  //Click the + button
  [app.navigationButton touch];

  //Set the form fields.
  //Also ".with" is optional so we here we can show the different syntax
  [[app.textField.with placeholder:@"First Name"] setText:@"Brian"];
  [[app.textField.with placeholder:@"Last Name"] setText:@"Knorr"];
  [[app.textField.with placeholder:@"Email"] setText:@"[email protected]"];
  [[app.textField placeholder:@"Username"] setText:@"bkuser"];
  [[app.textField placeholder:@"Password"] setText:@"test"];
  [[app.textField placeholder:@"Confirm"] setText:@"test"];

  //Click the Save button
  [[app.navigationButton.label text:@"Save"] touch];

  //Make sure the error alert view doesn't appear
  [app timeout:1].alertView.should.not.exist;

  //User list should now have a new entry
  [[app.tableView.label text:@"Brian Knorr"] should].exist;
}

@end

Keep in mind that I've never used it, so there's a chance it won't fit your needs exactly. But at the very least, you'll be able to use the codebase as inspiration for writing your own test framework.

Nathan de Vries
Project seems to be active and looks like what I need. Thanks!
Bragi Ragnarson
+2  A: 

Adam Milligan of Pivotal Labs has created a BDD framework for Objective-C called Cedar that targets both Cocoa and Cocoa Touch. It uses blocks in a similar way to RSpec. Here's an example specification:

SPEC_BEGIN(FooSpecs)

sharedExamplesFor(@"a similarly-behaving thing", ^(NSDictionary *context) {
    it(@"should do something common", ^{
        ...
    });
});

NSDictionary *context = [NSDictionary dictionary];

describe(@"Something that shares behavior", ^{
    itShouldBehaveLike(@"a similarly-behaving thing", context);
});

describe(@"Something else that shares behavior", ^{
    itShouldBehaveLike(@"a similarly-behaving thing", context);
});

SPEC_END
Don McCaughey