Ios 猕猴桃规格的全球帮助器
我在SPEC文件中的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
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"]; });
所有方法都要求您在单元测试目标可用标头中声明帮助程序,否则您将收到编译警告/错误。您有过这样的运气吗?