0
我想從第三方庫中嘲笑免費的C函數。我知道googlemock建議將函數作爲方法包裝在接口類中。我如何檢查googlemock中的字符串參數作爲void pointer
一些C函數期望void *參數,其解釋取決於上下文。在一個測試用例中,以0結尾的字符串用於void *參數。
在模擬對象中,我想檢查字符串的內容,當它作爲void *傳輸時。當我嘗試使用STREQ檢查字符串的內容,那麼它不工作:
error: no matching function for call to std::__cxx11::basic_string<char>::basic_string(void*&)
我不想改變從包裝中的數據類型void *爲char *,使這項工作,因爲通過此參數傳遞的數據也可以是其他內容。我可以通過googlemock的參數匹配器來檢查由void *指向的數據,最好比較字符串是否相等?
代碼。如果你爲函數Foo添加一些定義並且鏈接到gmock_main.a,那麼它會編譯,除了上面的錯誤。
#include <gmock/gmock.h>
// 3rd party library function, interpretation of arg depends on mode
extern "C" int Foo(void * arg, int mode);
// Interface class to 3rd party library functions
class cFunctionWrapper {
public:
virtual int foo(void * arg, int mode) { Foo(arg,mode); }
virtual ~cFunctionWrapper() {}
};
// Mock class to avoid actually calling 3rd party library during tests
class mockWrapper : public cFunctionWrapper {
public:
MOCK_METHOD2(foo, int(void * arg, int mode));
};
using ::testing::StrEq;
TEST(CFunctionClient, CallsFoo) {
mockWrapper m;
EXPECT_CALL(m, foo(StrEq("ExpectedString"),2));
char arg[] = "ExpectedString";
m.foo(arg, 2);
}