Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 猕猴桃规格的全球帮助器_Ios_Objective C_Testing_Kiwi - Fatal编程技术网

Ios 猕猴桃规格的全球帮助器

Ios 猕猴桃规格的全球帮助器,ios,objective-c,testing,kiwi,Ios,Objective C,Testing,Kiwi,我在SPEC文件中的BEGIN\u SPECEND\u SPEC块中定义了一些辅助块,我经常重复使用它们。例如,断言某个对话框出现: void (^expectOkAlert) (NSString *, NSString *) = ^void(NSString *expectedTitle, NSString *expectedMessage) { UIAlertView *alertView = [UIAlertView mock]; [UIAlertView stub:@se

我在SPEC文件中的
BEGIN\u SPEC
END\u 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那样

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

您可以

  • expectOkAlert
    声明为全局变量,在其他单元测试包含的公共头中

    extern void (^expectOkAlert) (NSString *, NSString *);
    
  • 或者在
    KWSpec
    类别中声明
    expectOkAlert
    ,您仍然需要一个包含在您需要使用它的单元测试中的公共头

    @implementation KWSpec(Additions)
    + (void)expectOkAlertWithTitle:(NSString*)title andMessage:(NSString*)message;
    @end
    
    你是这样使用它的:

    it(@"expects the alert", %{
        [self expectOkAlertWithTitle:@"a title" andMessage:@"a message"];  
    });
    
    it(@"expects the alert", %{
        [[UIAlertView should] showOkAlertWithTitle:@"a title" andMessage:@"a message"];  
    });
    
  • 或创建自定义匹配器并使用该匹配器断言:

    @interface MyAlertMatcher: KWMatcher
    - (void)showOKAlertWithTitle:(NSString*)title andMessage:(NSString*)message;
    @end
    
    并在您的测试中使用它,如下所示:

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

所有方法都要求您在单元测试目标可用标头中声明帮助程序,否则您将收到编译警告/错误。

您有过这样的运气吗?