5

Short version of the question:

What is wrong with the following Kiwi/iOS mock expectation?

[[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil];

Long version of question:

I am trying to write a test in Kiwi, iOS for a simple class that handles a NSConnection. To test that the class handles the callback from the NSConnection I send it the delegate methods NSConnection normally does. I have a delegate in the class that sends data back to whoever uses my class. To test my class I have to inject a mocked delegate and then check that my desired methods are called. Simple as that :)

My code for the Kiwi test is:

//Some ivars declared elsewhere:
testString1 = @"asd323/4 d14";
testString2 = @"as98 /2y9h3fdd14";
testData1 = [testString1 dataUsingEncoding:NSUTF8StringEncoding];
testData2 = [testString2 dataUsingEncoding:NSUTF8StringEncoding];
mockURLRespons = [NSHTTPURLResponse mock];
int value = 11111;
id mockDelegate = [KWMock mockForProtocol:@protocol(SharepointConnectionDelegate)];
communicator = [[SharepointCommunicator alloc] init];

it (@"should send recieve data back to delegate2", ^{
   [communicator setDelegate:mockDelegate];
   [mockURLRespons stub:@selector(statusCode) andReturn:theValue(value)];
   [(id)communicator connection:niceMockConnector didReceiveResponse:mockURLRespons];
   [(id)communicator connection:niceMockConnector didReceiveData:testData1];
   [(id)communicator connection:niceMockConnector didReceiveData:testData2];
   [(id)communicator connectionDidFinishLoading:niceMockConnector]; 

   [[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil];

});

And in my SharepointCommunicator.m:

-(void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response {
  if (connection != aConnection) {
      [connection cancel];
      connection = aConnection;
  }
  responseData = [[NSMutableData alloc] init];
  statusCode = [(NSHTTPURLResponse*)response statusCode];
}

-(void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data {
  if (aConnection != self.connection)
    return;
  [responseData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
  NSString *txt = [[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding];
  NSLog(@"Statuscode: %i", statusCode);
  NSLog(@"Data is: %@",txt);
  [delegate connectionDidSucceedWithText:txt andStatus:statusCode];
  [self.connection cancel];
  self.connection = nil;
}

This code works and is correct. Debugging it with checkpoint shows it does as expected. The values of statusCode is 11111. and txt is testString1+textString2. Still it fails on the last row on in the test with the following error:

error: -[kiwiSharepointCommunicatorTest Sharepointcommunicator_AStateTheComponentIsIn_ShouldSendRecieveDataBackToDelegate2] : 'Sharepointcommunicator, a state the component is in, should send recieve data back to delegate2' [FAILED], mock received unexpected message -connectionDidSucceedWithText:"asd323/4 d14as98 /2y9h3fdd14" andStatus:11111 
Test Case '-[kiwiSharepointCommunicatorTest Sharepointcommunicator_AStateTheComponentIsIn_ShouldSendRecieveDataBackToDelegate2]' failed (3.684 seconds).

Removing the last row in the test still generate the same error. I guess my understanding of receive:withArguments: is wrong..

Pooria Azimi
  • 8,585
  • 5
  • 30
  • 41
Sunkas
  • 9,542
  • 6
  • 62
  • 102
  • I am assuming it has something to do with Kiwi's matching of arguments. Perhaps the match is on the pointers to objects? And that's why the strings don't match, even if they contains the same strings? Or perhaps it can't handle matching of non-objects like ints... – Sunkas Apr 17 '12 at 08:14
  • I'm having a similar issue, my delegate is passing strings to its delegate (the mock) – quantumpotato Apr 20 '12 at 15:51

1 Answers1

6

You have to call [[mockDelegate should] receive... before the call to connectionDidFinishLoading to prepare the mockDelegate for the message it's about to receive.

Pieter Kuijpers
  • 7,247
  • 5
  • 28
  • 36
  • It was some time now since I coded this, but I'm quite sure you are correct. I did recode it a smaller way and got it working back then. Probably because the expectation now was set upfront. – Sunkas Oct 08 '12 at 12:44