[英]C++ gmock - How we can read/get the parameter value of a function of cpp file in unit test cpp file
我有一个 project_file.cpp 文件,如:
//Private Function definition
void my_fun(int &value)
{
//Do something
}
//Function Calling
int value = any_other_function();
myclass.my_fun(val);
现在我需要通过模拟 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);
}
}
上面的代码与全局变量“my_fun_val”一起工作正常,但我不想使用全局变量。 我们不能将函数 (my_fun()) 设为“公共”。 请指导其他方式在单元测试文件中获取参数。
解决上述问题的另一种方法,我们可以比较 ACTION 方法中的值。
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)));
}
}
注意:由于指针引用的使用,这里我们不能使用SaveArg()或SaveArgPointee() 。 当前 gmock 不提供任何方法/机制来读取作为引用传递的参数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.