Leem.fin
Leem.fin

Reputation: 42582

Check argument value passed into function in unit test

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.)

Upvotes: 1

Views: 931

Answers (1)

iSashok
iSashok

Reputation: 2446

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

Upvotes: 1

Related Questions