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
最後に、ヘルパーメソッドがanotheに存在する場合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