【问题标题】:Helper functions in Kiwi specsKiwi 规范中的辅助函数
【发布时间】:2012-06-25 22:42:24
【问题描述】:

我有一些重复的规格,我想把它们擦干。通用功能不适合移动到beforeEach 块中。本质上,它是对象创建,12 个对象各有 4 行,我想把这 4 行变成一个函数调用。

在 Kiwi 规范中我可以在哪里放置辅助函数?

在 RSpec 中,我可以将 def 放在规范块之间,但在这里似乎不可能。我什至尝试跳过SPEC_END 宏并自己添加该内容,以便我可以在SPEC_BEGIN 的@implementation 中添加函数,但这似乎也不起作用。

更正...我可以通过手动编码 SPEC_END 宏来管理类似的工作。我把结尾 } 放错了位置。但是,它仍然失败,因为该方法不在 @interface 中。

【问题讨论】:

标签: ios objective-c kiwi


【解决方案1】:

在 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

【讨论】:

  • 超级好用!非常感谢。
  • 是否可以有一个异步助手?假设我想在帮助程序中针对远程服务器进行身份验证。
【解决方案2】:

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

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

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

【讨论】:

  • 我可以发誓我试过了,但我不确定了。我得回去看看。这或多或少是我想要的。
  • 这给了我以下警告:警告:没有以前的原型函数'' [-Wmissing-prototypes] 这可以通过将方法设为静态来解决:static NSString *makeAString() { ... }
  • @SebastienMartin 我从未见过这个错误。你是在使用之前声明函数吗?
【解决方案3】:

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

最后,如果您的辅助方法位于另一个类中,gantaa 建议一个聪明的 hack:

@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-03-24
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2020-01-27
    • 1970-01-01
    相关资源
    最近更新 更多