Check argument value passed into function in unit test

為{幸葍}努か 提交于 2019-12-08 04:39:21

问题


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


回答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



来源:https://stackoverflow.com/questions/37706307/check-argument-value-passed-into-function-in-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!