gmock
Capture GMOCK string parameter
If I have the following interface member function: virtual bool print_string(const char* data) = 0; with the following mock MOCK_METHOD1(print_string, bool(const char * data)); Is it possible to capture the string that is passed to print_string()? I tried to: char out_string[20]; // SaveArg<0>(out_string); // this saves the first char of the sting this saves the first char of the sting but not the whole string.
Class struct Foo { virtual bool print_string(const char* data) = 0; }; Mock struct FooMock { MOCK_METHOD1(print_string, bool(const char * data)); }; Test struct StrArg { bool print_string(const char* data) { arg = data; return true; } string arg; }; TEST(FooTest, first) { FooMock f; StrArg out_string; EXPECT_CALL(f, print_string(_)) .WillOnce(Invoke(&out_string, &StrArg::print_string)); f.print_string("foo"); EXPECT_EQ(string("foo"), out_string.arg); } You can always use Invoke to capture parameter value in structure.
May be something like: char* p = out_string[0]; SaveArg<0>(&p); since SaveArg(pointer) save the Nth argument to *pointer. I think if you just need to validate the content of the array without deep copy, there is no need to create another array since only the pointer to the array could be obtained using this method.
Related Links
gmock ReturnRef returns compilation error
How to call function pointer which is passed to Mock method?
In gmock, Is there a way to have mock object return an instance of an user defined object?
GMock testing destructor calls
set EXPECT_CALL to redirect the call to the original method
Capture GMOCK string parameter
Mocking non-virtual method generating errors
gmock matcher doesn't match my arguments by reference
What did you do to solve gmock you mentioned (link enclosed)?
how to set custom ref-variable in gmock