Objective-C中的BDD

Bra*_*son 27 bdd objective-c

我最近开始学习Objective-C并使用与Xcode捆绑在一起的OCUnit编写我的测试.

我是一个很长时间的Ruby程序员,我习惯了RSpec和Cucumber - 很好的BDD框架.

在Objective-C中是否有一个像样的BDD框架?我想念我的'应该':)

djr*_*ero 21

我正在使用Kiwi Library Quick进行集成,效果非常好.

他们的github:

describe(@"Team", ^{
    context(@"when newly created", ^{
        it(@"should have a name", ^{
            id team = [Team team];
            [[team.name should] equal:@"Black Hawks"];
        });

        it(@"should have 11 players", ^{
            id team = [Team team];
            [[[team should] have:11] players];
        });
    });
});
Run Code Online (Sandbox Code Playgroud)


Nat*_*ies 17

有一个名为uispec的相对较新的项目受到RSpec测试DSL的启发.示例规范如下所示:

#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:@"b@g.com"];
  [[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
Run Code Online (Sandbox Code Playgroud)

请记住,我从未使用它,因此有可能它不能完全满足您的需求.但至少,您将能够使用代码库作为编写自己的测试框架的灵感.


Don*_*hey 14

Pivotal Labs的Adam Milligan为Objective-C创建了一个名为Cedar的BDD框架,该框架针对Cocoa和Cocoa Touch.它以与RSpec类似的方式使用块.这是一个示例规范:

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
Run Code Online (Sandbox Code Playgroud)


Chr*_*son 8

看看如何实现STAssertOCUnit(SenTestingKit,包含在Xcode中)中的宏.

在您自己的单元测试包中,您可以实现一个类别NSObject来添加类似假设的方法-shouldBeValid,然后调用与STAssert宏现在相同的通过/失败机制.

如果你不熟悉C预处理器......

您可能还必须使用a #define来为您的宏传递正确的值__FILE__以及__LINE__BDD测试失败时.例如,您可能必须执行以下操作:

@interface NSObject (BehaviorDrivenDevelopment)
- (void)shouldBeValidInFile:(const char *)file line:(int)line;
@end

#define shouldBeValid  shouldBeValidInFile:__FILE__ line:__LINE__
Run Code Online (Sandbox Code Playgroud)

那样你会像这样调用它:

[[someObject methodUnderTest:argument] shouldBeValid];
Run Code Online (Sandbox Code Playgroud)

编译器看到的代码是这样的:

[[someObject methodUnderTest:argument] shouldBeValidInFile:__FILE__ line:__LINE__];
Run Code Online (Sandbox Code Playgroud)

__FILE____LINE__预处理宏将扩大到目前的文件和行测试中的源文件.

这样,当您确实有一个失败的测试时,它可以将适当的信息传递给SenTestingKit以发送回Xcode.失败将在"构建结果"窗口中正确显示,单击它将带您进入测试中失败的确切位置.