title: Testing a AFNetworking API with Kiwi
body: <p>I'm writing some tests for a networking API with kiwi.</p>
  
  <p>Here is the relevant code:</p>
  
  <p>//////////////////////////////////////////////////////
  MyAPI.h
  //////////////////////////////////////////////////////</p>
  
  <pre><code>@protocol MyAPIDelegate&lt;NSObject&gt;
  
  -(void) onMyMethodCall:(id)response;
  -(void) onFail:(NSError*)error message:(NSString*)message;
  
  @end
  
  @interface MyAPI : NSObject
  
  @property (nonatomic, weak) id&lt;MyAPIDelegate&gt; delegate;
  
  -(void) myMethodCall;
  
  @end
  </code></pre>
  
  <p>//////////////////////////////////////////////////////
  MyAPI.m
  //////////////////////////////////////////////////////</p>
  
  <pre><code>@synthesize delegate    
  
  -(void) myMethodCall
  {
    NSDictionary *params = [Dictionary dictionaryWithObject:@"value1" forKey:@"param1"];
    [self apiCallWithServerPath:@"logic/myMethodCall"
                     parameters:params
                      onSuccess:^(id response) {
                          [delegate onMyMethodCall];
                      }
                      onFailure:^(NSError *error, NSString* msg) {
                          [delegate onFail:error message:msg];
                      }];
  }
  
  -(void) apiCallWithServerPath:(NSString *)serverPath parameters:(NSDictionary *)parameters onSuccess:(void (^)(id))success onFailure:(void (^)(NSError *, NSString *))failure
  {
      AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL urlWithString:@"http://www.myserver.com/"]];
      [client setParameterEncoding:AFFormURLParameterEncoding];
  
      [client postPath:serverPath
            parameters:parameters
               success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 id response = [[JSONDecoder decoder] objectWithData:responseObject];
                 success(response);
               }
               failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"API call failed:  error message = %@",[error localizedDescription]);
                  failure(error, [error localizedRecoverySuggestion]);
               }];
  }
  </code></pre>
  
  <p>//////////////////////////////////////////////////////
  MyAPITest.m
  //////////////////////////////////////////////////////</p>
  
  <pre><code>SPEC_BEGIN(MyAPITest)
  
  describe(@"MyAPITest should", ^{
      __block MyAPI *api;
      __block MyAPI *delegateMock;
  
      beforeEach(^{
          delegateMock = [KWMock mockForProtocol:@protocol(MyAPIDelegate)];
          api = [[MyAPI alloc] init];
          api.delegate = delegateMock;
      });
  
      afterEach(^{
          delegateMock = nil;
          api = nil;
      });
  
      it(@"should go to the server and get an answer, dont care about the value atm", ^{
          [[api should] receive:@selector(myMethodCall)];
  
          KWCaptureSpy *spy = [delegateMock captureArgument:@selector(onMyMethodCall:) atIndex:0];
  
          // I usually put a breakpoint here... 
          [api myMethodCall];
  
          [[expectFutureValue(spy.argument) shouldEventually] beNonNil];
      });
  });
  
  SPEC_END
  </code></pre>
  
  <p>This is a very simple test, that I'm building incrementally.  Eventually I'd like to test the values inside spy.argument, but for now i'm only interested in making sure that it is not nil.</p>
  
  <p>Running the test always fails with: [FAILED] Argument requested has yet to be captured.</p>
  
  <p><strong>Debugging doesn't work</strong>:  When I try to put breakpoint in the test (where the comment says) its <strong>never steps into</strong> the method.</p>
  
  <p>At the same time, if I put NSLog(s) inside MyAPI's myMethodCall, they are <strong>not printed on console.</strong></p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>/////////////////////// UPDATE //////////////////////////////////</p>
  
  <p>Turns out that if I comment/remove this line (from the test):</p>
  
  <pre><code>[[api should] receive:@selector(myMethodCall)];
  </code></pre>
  
  <p>The test works.  Any idea of why this line is causing the problem?</p>