Global Assistant for Kiwi Specifications

I defined some auxiliary blocks in a block BEGIN_SPEC END_SPECin my spec file, which I often use. For instance. claiming a specific dialog box appears:

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)];
};

I would like to reuse this block for several other specification files. Is it somehow possible, as we usually do with spec helpers and rspec in the Ruby world?

How do you manage global spec assistants?

+4
source share
1 answer

You can

  • declare expectOkAlertas a global variable in a common header that is included in other unit tests

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

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

    :

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

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

    :

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

, , / .

+2

Source: https://habr.com/ru/post/1540470/


All Articles