mardi 31 mai 2016

how to check the return value of a function in unit test with OCMock 3

I am using OCMock 3 to do unit testing in my iOS project.

OCMock is said a great unit testing library for iOS, but the OCMock document I linked above doesn't say clearly how to check the return value of a function, it all the time says how to stub function return & verify that. But I don't want to stub that function return, I need to check the real return value.

For example, I want to unit test a function of my School class:

@implementation School
...
- (void) checkStudents {
  BOOL isOnVacation = [[Coordinator sharedInstance] checkCalendar];
  if (!isOnVacation) {
     takeLecture();
  }
}
@end 

My test case:

- (void) testCheckStudents {
    // create a partially mocked 'Coordinator' instance
    id coordinatorMock = [OCMockObject partialMockForObject:[Cooridnator sharedInstance]];

    // run the method under test
    [schoolToTest checkStudents];

    // I want to check not only '[[Coordinator sharedInstance] checkCalendar]' is invoked, but also check its return value is YES. How to check this in OCMock?
    OCMVerify([coordinatorMock checkCalendar]);
}

I don't want to stub the return value of [[Coordinator sharedInstance] checkCalendar], but run the real implementation.

I want to check not only [[Coordinator sharedInstance] checkCalendar] is invoked, but also check its return value is YES. How to check this in OCMock?

(With its documentation, I can only see stub function return here & there then verify the function is called. Do I miss something in its documentation?)

Aucun commentaire:

Enregistrer un commentaire