Question

Im a beginner to OCMock and I need to verify if a method is invoked from another class. The following is my code.

 //Creating the OCMockObject
id mockProductRequest = [OCMockObject mockForClass:[ProductRequest class]];
[[mockProductRequest expect] testProductRequest];

//Creating the object where the mock object will be invoked
ProductService *actualService = [[ProductService alloc] init];
[actualService testProductService];

[mockProductRequest verify];

-(void)testProductService{
//Method where the mock object's method is invoked
ProductRequest *request = [[ProductRequest alloc] init];
[request testProductRequest];
}

I seems to always receive a method was not invoked exception. Please help me to figure out what I'm doing wrong here.

Was it helpful?

Solution

You have to replace the real request inside the real ProductService. You can archiev this for example by making the request a property of the ProductService like this:

@interface ProductService
@property (strong) ProductRequest *request

and then exchange the real ProductRequest with the mocked one like this

- (void)testProductService {
  id mockProductRequest = [OCMockObject mockForClass:[ProductRequest class]]; 
  [[mockProductRequest expect] testProductRequest];

  ProductService *actualService = [[ProductService alloc] init]; 
  actualService.request = mockProductReqeust;

  // call some method on actualService which invokes the request

  [request testProductRequest];


}

OTHER TIPS

The aim of Mock Objects is to replace the objects you want to test. But in your code you send the message testProductRequest to a real ProductRequest and not to your MockObject !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top