14

I have a few repetitive specs that I would like to DRY up. The common functionality doesn't lend itself to moving into a beforeEach block. Essentially, it's object creation and is 4 lines for each of 12 objects, I'd like to turn those 4 lines into a single function call.

Where can I put helper functions in a Kiwi spec?

In RSpec I can just put def between spec blocks, but that doesn't appear to be possible here. I've even tried skipping the SPEC_END macro and adding that content myself so I could add functions inside the @implementation from SPEC_BEGIN but that doesn't seem to work, either.

Correction... I can manage something that kind of works with hand-coding the SPEC_END macro. I had the end } mis-placed. But still, it fails, because the method isn't in the @interface.

4

3 回答 3

34

在 SPEC_BEGIN 之后将您的辅助函数创建为块:

SPEC_BEGIN(MySpec) 

NSDate* (^dateFromString) (NSString *) = ^NSDate* (NSString *dateString) {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateFormat:@"MM-dd-yyyy"];
    return [dateFormatter dateFromString:dateString];
};


describe(@"something", ^{
    NSDate *checkDate = dateFromString(@"12-01-2005");

...
});

SPEC_END
于 2012-08-03T18:09:36.790 回答
8

SPEC_BEGIN()您还可以在范围 上方创建一个直接的 C 函数。

NSString *makeAString () {
    return @"A String";
}

或者,如果您有将在多个 Spec 文件中使用的辅助函数,请将这些函数放在单独的文件中并导入头文件。我发现这是清理我的 Specs 的好方法。

于 2012-12-21T20:59:11.560 回答
5

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

另一种选择是道格建议

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

最后,如果您的辅助方法位于另一个类中,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
于 2014-11-27T10:00:37.087 回答