简体   繁体   中英

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?

Here's a possible scenario:

  • you're consuming a RESTful webservice that allows you to update your profile by doing a POST /user with the details you want to update.
  • you have a HNKUser class that declares an updateFirstName:lastName: method that calls the webservice
  • you want to make sure that the method will send only the firsName and lastName to the server (eg it doesn't also send birthday and other details)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM