Specify帮助您用对话方式描述行为。
它是作为一种个人简单BDD库的需要而开发的,比替代品更轻量且可扩展,还增加了额外的DSL函数来简化示例的编写。
Specify利用块的动态特性来定义类似 it()
和 describe()
的DSL方法
describe(@"the stack", ^{
__block Stack *stack;
before(^{
stack = [[Stack alloc] init];
});
context(@"when newly initialized", ^{
it(@"is empty.", ^{
[[[stack popObject] should] beNil];
});
});
context(@"when 1 object is pushed", ^{
before(^{
[stack pushObject:@1];
});
describe(@"first pop", ^{
it(@"returns the object.", ^{
[[[stack popObject] should] beEqualTo:@1];
});
});
describe(@"second pop", ^{
it(@"returns nil", ^{
[stack popObject];
[[[stack popObject] should] beNil];
});
});
});
});
这使得实现自定义DSL变得更加容易。如果您更习惯于使用 test
术语来定义示例,您可以定义它: void(^test)(NSString *, INLVoidBlock) = it;
,然后使用它
void(^test)(NSString *, INLVoidBlock) = it;
describe(@"the stack", ^{
test(@"stack is initially empty.", ^{
[[[stack popObject] should] beNil];
});
});
毕竟,如果 语言是如此重要,我们就应该能说出我们想说的话。
Specify提供了一些额外的便利函数,以帮助编写规范。要查看函数和示例的完整列表,请参阅Wiki。
上面的示例使用了Posit提供的“should”语法,但您可以使用支持SenTestingKit的任何框架,包括Expecta。您还可以使用默认的SenTestingKit宏
it(@"is empty.", ^{
// SenTestingKit syntax
STAssertNil([stack popObject], @"Popping an empty stack should return nil."); // SenTestingKit
// Posit syntax
[[[stack popObject] should] beNil];
// Expecta syntax
expect([stack popObject]).to.beNil();
});
使用CocoaPods通过在Podfile中添加 pod 'Specify', '~> 0.1.1'
来安装Specify。
Copyright (c) 2013 Ryan Davies
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.