我最近开始学习Objective-C,并使用与Xcode捆绑在一起的OCUnit编写我的测试。BDD in Objective-C
我是一个很长时间的Ruby程序员,我习惯于RSpec和黄瓜 - 很好的BDD框架。
在Objective-C中是否有一个体面的BDD框架?我错过了我的'应该的:)
我最近开始学习Objective-C,并使用与Xcode捆绑在一起的OCUnit编写我的测试。BDD in Objective-C
我是一个很长时间的Ruby程序员,我习惯于RSpec和黄瓜 - 很好的BDD框架。
在Objective-C中是否有一个体面的BDD框架?我错过了我的'应该的:)
有一个相对较新的项目叫做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:@"[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
请记住,我从来没有使用过它,所以有可能它不会完全符合您的需求。但至少,您可以使用代码库作为编写您自己的测试框架的灵感。
没有什么能阻止你在你的测试方法前加上Should。我在C#中用NUnit做到了这一点。
我所追求的是验证的特定语法。在Ruby中,它看起来像: method_under_test(args)。should be_valid – 2009-08-23 19:01:26
问这个问题的人在谈论Objective-C和OCUnit,它们期望测试方法本身以“test”开始 - 这就是它知道测试方法的方式方法,因为Objective-C没有像C#和Java那样的注释。 – 2009-09-26 20:16:51
看看OCUnit中的STAssert
宏(SencodeestKit,包含在Xcode中)是如何实现的。
在您自己的单元测试包中,您可以在NSObject
上实现一个类别,以添加类似假设的-shouldBeValid
的方法,然后调用与STAssert
宏相同的通过/失败机制。
如果你不是十分熟悉的C预...
你可能还需要使用您的宏的#define
通过正确的价值观以通为__FILE__
和__LINE__
当你的BDD测试失败。例如,你可能需要做这样的事情:
@interface NSObject (BehaviorDrivenDevelopment)
- (void)shouldBeValidInFile:(const char *)file line:(int)line;
@end
#define shouldBeValid shouldBeValidInFile:__FILE__ line:__LINE__
这样,你会调用它是这样的:
[[someObject methodUnderTest:argument] shouldBeValid];
的代码编译器看到的将是这样的:
[[someObject methodUnderTest:argument] shouldBeValidInFile:__FILE__ line:__LINE__];
__FILE__
和__LINE__
预处理器宏将扩展为测试源文件中的当前文件和行。
这样,当你有一个失败的测试,它可以传递适当的信息SenTestingKit发送回Xcode。失败将在Build Results窗口中正确显示,点击它会将您带到测试中失败的确切位置。
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
我使用Kiwi Library快速整合,效果很好。
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];
});
});
});
您可以在BDD From the idea to the app看看在那里你会使用蠡看一个例子。
项目似乎很活跃,看起来像我需要的东西。谢谢! – 2009-08-25 11:09:52
谢谢,这是非常好的。 – 2011-04-28 04:11:52