4

I'm new to OCMock and I'm planing to mock a request call. The API that needs to be mocked is executed as defined below.

   [ProductRequest requestProductUpdateUrl: @"testUrl" withParameters:params   error:^(NSString *updateUrl, NSError *error){
        if (!error && [updateUrl length] !=0 ) {
           NSLog(@"Success");
        } else {
             NSLog(@"Error");
        }
   }];

Any idea on how I can mock the method requestProductUpdateUrl using OCMock ?

g_fred
  • 5,878
  • 3
  • 28
  • 36
Jani
  • 1,400
  • 17
  • 36
  • It looks like `requestProductUpdateUrl` is a class method. Is that correct? You can't mock class methods with OCMock. – Christopher Pickslay Oct 11 '12 at 23:18
  • @Christopher Yes, It is a class method but I can modify it not to be a class method too. Thank you for pointing it out. If so my code will be modified self.productService = [OCMockObject mockForClass:[ProductService class]]; [self.productService requestProductUpdateUrl: @"testUrl" withParameters:params error:^(NSString *updateUrl, NSError *error){ if (!error && [updateUrl length] !=0 ) { NSLog(@"Success"); } else { NSLog(@"Error"); } }]; – Jani Oct 12 '12 at 04:03
  • OK, so what do you want to test? Do you want to just verify that it's invoked, that it's invoked with a certain block, etc? – Christopher Pickslay Oct 12 '12 at 06:05
  • I want to provide a mock stub for the method – Jani Oct 12 '12 at 07:01

1 Answers1

0

This feature is not yet implemented, although I think they are working on it. I had to do something like this too:

semaphore = dispatch_semaphore_create(0);
[myObject myFunctionWithCallback:^(BOOL success){
     if(success)
        dispatch_semaphore_signal(semaphore);
     else
        STFail(@"Call failed"); dispatch_semaphore_signal(semaphore);

}];
[self waitForSemaphore];

With the wait function something like this:

- (void)waitForSemaphore;
{
float step_duration = .1;
float wait_steps = 2 / step_duration;
while (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 10)) && wait_steps > 0){
    CFRunLoopRunInMode(kCFRunLoopDefaultMode,step_duration, YES);
    wait_steps--;
}

if (wait_steps <= 0) {
    STFail(@"Timeout!");
}

}

Tim Specht
  • 3,068
  • 4
  • 28
  • 46