1

I am using OCMock v3 do unit testing, I want to test the following piece of code:

@implementation School
-(void) handleStudent:(Student*) student{
 Bool result = [self checkIdentityWithName:student.name age:student.age];
 ...
}
...
@end

In my following test case I created a student instance with name "John", age 23. and then I run the function under test:

-(void) testHandleStudent{
  Student *student = [Student initWithName:@"John" age:23];
  // function under test
  [schoolPartialMock handleStudent:student];

  // I want to not only verify checkIdentityWithName:age: get called, 
  // but also check the exact argument is passed in. that's John 23 in this case
  // how to check argument ? 

}

In my test case, I want to verify that the exact arguments values are passed into function checkIdentityWithName:age: . that's name "John" and age 23 are used. How to verify that in OCMock v3? (There is no clear example in its documentation how to do it.)

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

1 Answers1

1

You can make it like that

-(void) testHandleStudent{
    id studentMock = OCMClassMock([Student class]);
    OCMStub([studentMock name]).andReturn(@"John");
    OCMStub([studentMock age]).andReturn(23);

    [schoolPartialMock handleStudent:studentMock];
    OCMVerify([schoolPartialMock checkIdentityWithName:@"John" age:23]);
}

or

-(void) testHandleStudent{
        id studentMock = OCMClassMock([Student class]);
        OCMStub([studentMock name]).andReturn(@"John");
        OCMStub([studentMock age]).andReturn(23);

        OCMExpect([schoolPartialMock checkIdentityWithName:@"John" age:23]);

        [schoolPartialMock handleStudent:studentMock];

        OCMVerifyAll(schoolPartialMock);
    }

Hope this help

iSashok
  • 2,326
  • 13
  • 21
  • thanks, I'd like to see any other answer could provides a solution to capture the argument on the fly with OCMock 3. If impossible, I will accept this answer. – Leem.fin Jun 08 '16 at 18:45
  • Thanks but If you look OCMock 3 documentation the answer that i provided matches this documentation and syntaxis. – iSashok Jun 08 '16 at 19:53
  • This is the correct answer. If you really want to capture the arguments to do asserts then you have to use blocks using `andDo`. In that case you have to take extra care with scoping of variables and with retaining the arguments pulled from the invocation. Questions have been asked (and answered!) around this before. – Erik Doernenburg Jun 09 '16 at 13:52
  • Helped to get an idea for my scenario, thanks very much ! – Naishta Jul 30 '18 at 22:28