首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kiwi规范的全局帮助器

Kiwi规范的全局帮助器
EN

Stack Overflow用户
提问于 2014-05-14 14:25:24
回答 1查看 145关注 0票数 4

我在我的spec文件中的BEGIN_SPEC END_SPEC块中定义了一些帮助器块,我经常重用它们。例如,断言某个对话框出现:

代码语言:javascript
复制
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世界中使用spec helper和rspec一样呢?

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

EN

回答 1

Stack Overflow用户

发布于 2015-05-22 13:57:19

你可以的

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

extern void (^expectOkAlert) (NSString *,NSString *);

  • 或在KWSpec类别中声明expectOkAlert,您仍然需要一个包含在单元测试中的公共头文件才能使用它

@implementation KWSpec(Additions) +(Void)expectOkAlertWithTitle:(NSString*)标题消息:(NSString*)andMessage;@end

你可以这样使用它:

it(@“期望警报”,%{ self expectOkAlertWithTitle:@"a title“andMessage:@"a

  • ”;});

  • 或创建自定义匹配器并使用它来断言:

@接口MyAlertMatcher: KWMatcher -(空)showOKAlertWithTitle:(NSString*)标题andMessage:(NSString*)消息;@end

并在您的测试中使用它,如下所示:

it(@“期待警报”,%{ [UIAlertView应该显示showOkAlertWithTitle:@“标题”和消息:@“消息”];});

所有方法都需要在单元测试目标可用标头中声明帮助器,否则将出现编译警告/错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23647103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档