C 字符串永远不会在 gmock 中调用
C-string will never be called in gmock
我有一个C++函数要测试,在这个C++函数中,它将调用一个C函数。我想用Gmock来模拟这个C函数。C 函数的签名为:
int functionA(const char key[3],char value[6]);
int
是返回码,输入key
,输出value
。我想模拟返回值并设置第二个参数。
我想测试C++函数是:
int functionB{
....
int rcode = functionA(key, value);
.......
}
我的"模拟"是:
#include<functionA.h>
#include <gmock/gmock.h>
class function_A_interface {
public:
virtual int functionA(const char key[3],char value[6]) = 0;
};
class function_A_mock : public function_A_interface {
public:
MOCK_METHOD2(functionA, int(const char[3], char[6]));
}
我的"mock.cc"是:
#include <function_A_mock.h>
extern function_A_mock MockObj;
int function_A(const char key[3],char value[6])
{
return MockObj.functionA(key, value);
}
我的"test.t.cpp"是:
TEST(Test, tes){
function_A_mock MockObj;
pa[3] = {0};
pb[6] = {1};
EXPECT_CALL(MockObj, functionA(pa, pb)).WillOnce(DoAll(SetArgPointee<1>(*set), Return(3)));
functionB(parameter1, parameter2); // it will convert these two paramters to according input in functionA
}
但它向我展示了:
Unexpected mock function call - returning default value.
Function call: functionA(ffbfc5b0 pointing to "abc E", ffbfc570 pointing to "")
Returns: 0
Google Mock tried the following 1 expectation, but it didn't match:
test.t.cpp:121: EXPECT_CALL(MockObj, functionA(pa, pb))...
Expected arg #0: is equal to ffbfd380 pointing to "abc E"
Actual: ffbfc5b0 pointing to "abc E"
Expected arg #1: is equal to ffbfd340 pointing to ""
Actual: ffbfc570 pointing to ""
Expected: to be called once
Actual: never called - unsatisfied and active
似乎我永远无法匹配我设置的参数,因此永远不会调用它。在我的情况下,如何模拟 C 字符串以进行匹配和设置参数目的?谢谢。
如果您不提供任何其他匹配器来模拟函数参数,则假定Eq()
。 Eq()
匹配器非常简单 - 它使用 operator ==
将收到的参数与预期的参数进行比较。但是对于 C 样式字符串,此比较比较的是指针,而不是实际字符串。
您应该使用匹配器来比较StrEq
:
using ::testing::StrEq;
EXPECT_CALL(MockObj, functionA(StrEq(pa), StrEq(pb)))
.WillOnce(DoAll(SetArgPointee<1>(*set), Return(3)));
但是,StrEq()
会将内容作为以 null 结尾的字符串进行比较。如果要比较整个数组,请使用ElementsAreArray(array, count)
:
EXPECT_CALL(MockObj, functionA(ElementsAreArray(pa, 3), ElementsAreArray(pb, 6)))
.WillOnce(DoAll(SetArgPointee<1>(*set), Return(3)));
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 使用 gmock c++ 在真实对象上调用方法
- Gmock 常量方法不调用,而是调用原始方法
- C 字符串永远不会在 gmock 中调用
- 使用 gtest 和 gmock 验证程序调用
- gmock EXPECT_CALL 不会在测试类中调用该方法
- 对隐式删除的复制构造函数的 gmock 调用
- 如何使用gmock框架模拟c++单元测试中的fork和execlp系统调用
- sleep() 在 gmock 的EXPECT_CALL内调用
- 如何将 GMock EXPECT_CALL设置为为模拟函数调用两个不同的函数
- GMock:EXPECT_CALL未按预期调用File*
- gmock 忽略"interesting"函数调用
- c++gmock使用相同的args调用其他函数
- Gmock调用函数两次
- Gmock只需要一个特定的调用
- 如何使用 gmock 来测试类是否调用其基类的方法
- gmock:在每次模拟调用中返回不同的值
- GMock将一个模拟对象传递给另一个对象,并调用一个stubed方法,这仍然是在调用真正的逻辑