class method mocking in NSURLConnection

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

class method mocking in NSURLConnection

Postby raresp » 26 Jul 2013, 14:51

Hello everyone,

I'm new to OCMock, good work by the way!

Trying to mock a class method of NSURLConnection and it doesn't seem to work.

Here is my code:
Code: Select all
NSString* str = @"teststring";
    NSData* responseData = [str dataUsingEncoding:NSUTF8StringEncoding];
   
    NSError* err = [OCMArg any];
   
    NSURLResponse * response = [OCMArg any];
     id mock = [OCMockObject mockForClass:[NSURLConnection class]];
    [[[mock stub] andReturn:responseData] sendSynchronousRequest:[OCMArg any] returningResponse:&response error:&err];
   
    NSData* rData = [NSURLConnection sendSynchronousRequest:nil returningResponse:&response error:&err];
    XCTAssertEqualObjects(rData, responseData, @"Mocking doesn't work");


The test fails and debugging reveals that handleInvocation in OCMReturnValueProvider is never called.
Am I missing something?
Does OCMock work with class methods that have arguments passed by reference?

Thank you,
Rares
raresp
 
Posts: 2
Joined: 26 Jul 2013, 14:43

Re: class method mocking in NSURLConnection

Postby raresp » 26 Jul 2013, 15:02

Figured it out.
Code: Select all
NSString* str = @"teststring";
    NSData* responseData = [str dataUsingEncoding:NSUTF8StringEncoding];
   
    NSError* err;
   
    NSURLResponse * response = [[NSURLResponse alloc] init];
   
    id mock = [OCMockObject mockForClass:[NSURLConnection class]];
    [[[mock stub] andReturn:responseData] sendSynchronousRequest:[OCMArg any] returningResponse:[OCMArg setTo:response] error:[OCMArg setTo:nil]];
   
    NSData* rData = [NSURLConnection sendSynchronousRequest:nil returningResponse:&response error:&err];
    XCTAssertEqualObjects(rData, responseData, @"Mocking doesn't work");
raresp
 
Posts: 2
Joined: 26 Jul 2013, 14:43


Return to OCMock