检查在单元测试中传递给函数的参数值 [英] Check argument value passed into function in unit test
本文介绍了检查在单元测试中传递给函数的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 OCMock v3 做单元测试,我想测试下面这段代码:>
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
在我的以下测试用例中,我创建了一个名为John"的 student
实例,年龄为 23.然后我运行测试中的函数:
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 ?
}
在我的测试用例中,我想验证 确切参数值 是否已传递到函数 checkIdentityWithName:age:
中.使用的是名字John"和年龄 23.如何在 OCMock v3 中验证?(在它的文档中没有明确的例子如何去做.)
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.)
推荐答案
你可以这样做
-(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]);
}
或
-(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);
}
希望对您有帮助
这篇关于检查在单元测试中传递给函数的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文