Search code examples
iosunit-testingpass-by-referencekiwi

iOS Kiwi test case with error passed via reference


let's say we have:

 - (NSString *)someMethod:(NSString *)input
 {
      NSError *error = nil;
      NSString *result = [otherObject processInput:input error:&error];

      if (error == nil) {
           return result;
      }
      else {
           return nil;
      }
 }

How can I unit test it using Kiwi to check how else is behaving?

Of course I could put something nasty as input but I don't want to use that approach. It's unit test for someMethod: method, not for processInput:error: method and otherObject is a KWMock. I've been trying to use KWCaptureSpy class, or stub: withArguments: but either I've used them wrong or they are not the solution.

Regards


Solution

  • In Cocoa you should check if a method returns nil, not check error against nil: http://rentzsch.tumblr.com/post/260201639/nserror-is-hard.

    So maybe rewrite someMethod to check value returned by otherObject stub, not the error pointer. It will be easier to test that way (you only have to call stub:andReturn: method from KWMock, returning either nil or a non-nil value) and you will follow Cococa conventions.