全球Kiwi规格助手

4

我在规范文件中的BEGIN_SPECEND_SPEC块中定义了一些常用的帮助块。例如,断言某个对话框是否显示:

void (^expectOkAlert) (NSString *, NSString *) = ^void(NSString *expectedTitle, NSString *expectedMessage) {
    UIAlertView *alertView = [UIAlertView mock];
    [UIAlertView stub:@selector(alloc) andReturn:alertView];
    [[alertView should] receive:@selector(initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:)
                      andReturn:alertView
                  withArguments:expectedTitle,expectedMessage,any(),@"OK",any()];
    [[alertView should] receive:@selector(show)];
};

我希望能够在其他几个规范文件中重用此代码块,类似于 Ruby 世界中的规范助手和 RSpec。是否有可能实现?

您如何管理全局规范助手?

1个回答

2
你可以:
  • declare expectOkAlert as a global variable, in a common header that gets included by the other unit tests

    extern void (^expectOkAlert) (NSString *, NSString *);
    
  • or declare expectOkAlert in a KWSpec category, you'll still need a common header that gets included in the unit tests you need to use it

    @implementation KWSpec(Additions)
    + (void)expectOkAlertWithTitle:(NSString*)title andMessage:(NSString*)message;
    @end
    

    and you use it like this:

    it(@"expects the alert", %{
        [self expectOkAlertWithTitle:@"a title" andMessage:@"a message"];  
    });
    
  • or create a custom matcher and use that to assert:

    @interface MyAlertMatcher: KWMatcher
    - (void)showOKAlertWithTitle:(NSString*)title andMessage:(NSString*)message;
    @end
    

    and use it in your tests like this:

    it(@"expects the alert", %{
        [[UIAlertView should] showOkAlertWithTitle:@"a title" andMessage:@"a message"];  
    });
    

所有方法都需要在可用的单元测试目标头文件中声明助手,否则您将收到编译警告/错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接