supermarin提出以下方法:
@implementation KWSpec(Additions)
+ (void)myHelperMethod:(Car*)car {
[[car shouldNot] beNil];
};
@end
SPEC_BEGIN(FooBarSpec)
describe(@"A newly manufactured car", ^{
it(@"should not be nil", ^{
[self myHelperMethod:[CarFactory makeNewCar]];
});
});
SPEC_END
另一種選擇是Doug suggests:
SPEC_BEGIN(FooBarSpec)
void (^myHelperMethod)(Car*) = ^(Car* car){
[[car shouldNot] beNil];
};
describe(@"A newly manufactured car", ^{
it(@"should not be nil", ^{
myHelperMethod([CarFactory makeNewCar]);
});
});
SPEC_END
關於它的好處是,它更傾向於很好地到異步場景:
SPEC_BEGIN(FooBarSpec)
__block BOOL updated = NO;
void (^myHelperAsync)() = ^()
{
[[expectFutureValue(theValue(updated)) shouldEventually] beYes];
};
describe(@"The updater", ^{
it(@"should eventually update", ^{
[[NSNotificationCenter defaultCenter] addObserverForName:"updated"
object:nil
queue:nil
usingBlock:^(NSNotification *notification)
{
updated = YES;
}];
[Updater startUpdating];
myHelperAsync();
});
});
SPEC_END
最後,如果你的幫手方法駐留在其他R類別,gantaa提出一個聰明的黑客:
@interface MyHelperClass
+(void)externalHelperMethod:(id)testCase forCar:(Car*)car
{
void (^externalHelperMethodBlock)() = ^(){
id self = testCase; //needed for Kiwi expectations to work
[[car shouldNot] beNil];
};
externalHelperMethodBlock();
}
@end
SPEC_BEGIN(FooBarSpec)
describe(@"A newly manufactured car", ^{
it(@"should not be nil", ^{
[MyHelperClass externalHelperMethod:self forCar:[CarFactory makeNewCar]];
});
});
SPEC_END
看到這個https://github.com/kiwi-bdd/Kiwi/issues/138 – onmyway133