[英]C++ gmock - How we can read/get the parameter value of a function of cpp file in unit test cpp file
I have a project_file.cpp file like:我有一个 project_file.cpp 文件,如:
//Private Function definition
void my_fun(int &value)
{
//Do something
}
//Function Calling
int value = any_other_function();
myclass.my_fun(val);
Now I need to fetch val parameter value through mock my_fun() method as below:现在我需要通过模拟 my_fun() 方法获取 val 参数值,如下所示:
namespace TEST
{
int my_fun_val; //global variable
class myTest : public Test
{
//Do something
};
ACTION(getMyFunctionVal)
{
my_fun_val = arg0; //copy val value
}
TEST_F(myTest, readValueOfFunction)
{
EXPECT_CALL(m_MyFunMock, my_fun(_)).WillOnce(::testing::DoAll(getMyFunctionVal(),
Return(true)));
EXPECT_EQ(my_fun_val, 5);
}
}
Above code work fine with global varibale "my_fun_val" but I don't want to use global variable.上面的代码与全局变量“my_fun_val”一起工作正常,但我不想使用全局变量。 And we cannot make function (my_fun()) as "public".
我们不能将函数 (my_fun()) 设为“公共”。 Please guide other way to get the parameter at unit test file.
请指导其他方式在单元测试文件中获取参数。
Another way to solve above problem, we can compare the value inside the ACTION method.解决上述问题的另一种方法,我们可以比较 ACTION 方法中的值。
utc_file.cpp utc_file.cpp
namespace TEST
{
class myTest : public Test
{
//Do something
};
ACTION_P(getMyFunctionVal, value)
{
EXPECT_EQ(arg0, value); //Here we can compare the value but unable to fill it as in reference variable and pass to callie method.
}
TEST_F(myTest, readValueOfFunction)
{
int expectedValue = 5;
EXPECT_CALL(m_MyFunMock, my_fun(_)).WillOnce(::testing::DoAll(getMyFunctionVal(expectedValue),
Return(true)));
}
}
Note: Here we cannot able to use SaveArg() OR SaveArgPointee() due to the Pointer reference usage.注意:由于指针引用的使用,这里我们不能使用SaveArg()或SaveArgPointee() 。 Currently gmock doesn't provide any method/mechanism to read parameter value when passed as a reference.
当前 gmock 不提供任何方法/机制来读取作为引用传递的参数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.