简体   繁体   English

基于参数值的C ++伪造/模拟返回值

[英]C++ fake/mock return value based on the value of parameter

Using Typemock Isolator++. 使用Typemock Isolator ++。 Is it possible that the returning value from a fake method is based on the value of parameter? 伪方法的返回值是否可能基于参数的值?

for example: 例如:

WHEN_CALLED(student->GradeOfCourse("a")).ReturnVal(70);
WHEN_CALLED(student->GradeOfCourse("b")).ReturnVal(85);

then if the parameter is ("a"), it will return 70. 那么如果参数是(“ a”),它将返回70。

and if the parameter is ("b") then it will return 85. 如果参数为(“ b”),则返回85。

Disclaimer I work in Typemock 免责声明我在Typemock工作

Use DoMemberFunctionInstead or DoStaticOrGlobalInstead to redirect your call to an alternative method - there you can return a differnet value based on the value of parameter. 使用DoMemberFunctionInsteadDoStaticOrGlobalInstead将您的调用重定向到其他方法-您可以在此基于参数的值返回differentnet值。

Add this method 添加此方法

int FakeGradeOfCourse(char * name)
{
   if (strcmp(name,"a")==0)
   {
      return 70;
   }
   return 85;
 }

In your test call: 在您的测试电话中:

WHEN_CALLED(student->GradeOfCourse(_)).
   DoStaticOrGlobalInstead(FakeGradeOfCourse, NULL);

You can find an example for this in our docs . 您可以在我们的文档中找到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM