iosobjective-cunit-testingtestingkiwi

What is a practical example of using Kiwi's KWCaptureSpy?


I'm having trouble understanding what a practical application of using Kiwi's KWCaptureSpy is. I could do something like this and have it pass:

 __block id successJSON;

  KWCaptureSpy *successBlockSpy =
      [HNKServer captureArgument:@selector(GET:parameters:completion:)
                         atIndex:2];

  [[HNKServer sharedServer] GET:@""
                     parameters:nil
                     completion:^(id JSON, NSError *error) {
                       successJSON = JSON;
                     }];

  HNKServerRequestCallback successBlock = successBlockSpy.argument;
  successBlock(@"JSON", nil);

  [[successJSON shouldEventually] equal:@"JSON"];

but that doesn't seem to actually be testing anything. The example in Kiwi's documentation doesn't help: https://github.com/kiwi-bdd/Kiwi/wiki/Mocks-and-Stubs#capturing-arguments

Has anyone had a good reason to use KWCaptureSpy in practice?


Solution

  • Here's a possible scenario:

    Supposing the method in discussion looks like this (I've omitted the completion handlers for simplicity):

    - (void)updateFirstName:(NSString*)firstName lastName:(NSString*)lastName {
        // preparation code
        // ...
        [serverApi POST:@"/user" parameters:someParamsYouveBuiltInTheMethod completion:someCompletionHandler];
        // ...
    }
    

    then you might want to capture the second argument and make sure that it contains only the firstName and lastName fields, and also that those fields have the proper value.

    As a note, spies are recommended to be used on mocks, and from your example I think yours is not.