检查在单元测试中传递给函数的参数值

时间:2016-06-08 15:05:48

标签: ios objective-c unit-testing ocmock

我正在使用OCMock v3进行单元测试,我想测试以下代码:

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

在我的下一个测试用例中,我创建了一个名为" John",23岁的student实例,然后运行测试中的函数:

-(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:。这个名字"约翰"和年龄23岁。如何在 OCMock v3 中验证? (其文档中没有明确的示例如何执行此操作。)

1 个答案:

答案 0 :(得分:1)

你可以这样做

public static String getStyledFont(String html) {
    boolean addBodyStart = !html.toLowerCase().contains("<body>");
    boolean addBodyEnd = !html.toLowerCase().contains("</body");
    return "<style type=\"text/css\">@font-face {font-family: CustomFont;" +
            "src: url(\"file:///android_asset/fonts/Brandon_reg.otf\")}" +
            "body {font-family: CustomFont;font-size: medium;text-align: justify;}</style>" +
            (addBodyStart ? "<body>" : "") + html + (addBodyEnd ? "</body>" : "");
}

-(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]);
}

希望这个帮助