我在我的spec文件中的BEGIN_SPEC END_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世界中使用spec helper和rspec一样呢?
你如何管理你的全局规范助手?
发布于 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:@“标题”和消息:@“消息”];});
所有方法都需要在单元测试目标可用标头中声明帮助器,否则将出现编译警告/错误。
https://stackoverflow.com/questions/23647103
复制相似问题