How to simulate variational functions with googlemock

Not as many questions as knowledge sharing.

According to GoogleMock, frequently asked questions cannot be used to mock variational functions because it is not known how many arguments will be passed to the function.

This is true, but in most cases it is known how many variables the function variable is called from the system test or how to convert variable arguments into one non-invariant argument.
My colleague (I don’t know if he is active on Stackoverflow) came up with a working solution, as shown in the example below (using the layout for the C-type interface):

class MockInterface { public: MockInterface() {} ~MockInterface() {} MOCK_METHOD4( variadicfunction, void( const std:: string name, AN_ENUM mode, const std::string func_name, const std::string message ) ); }; boost::shard_ptr<MockInterface> mock_interface; extern "C" { void variadicfunction( const char *name, AN_ENUM mode, const char *func_name, const char *format, ... ) { std::string non_variadic(""); if (format != NULL ) { va_list args; va_start( args, format ); // Get length of format including arguments int nr = vsnprintf( NULL, 0, format, args ); char buffer[nr]; vsnprintf( buffer, nr+1, format, args ); non_variadic = std::string( buffer ); va_end( args ); } mock_interface->variadicfunction( name, mode, func_name, non_variadic ); } } 

Hope this is helpful.

+7
googlemock
source share

No one has answered this question yet.

See similar questions:

2
GMOCK - a method that takes variable arguments

or similar:

7
googlemock - makes fun of a method that returns a complex data type
3
Passing arguments to ReturnNew using googlemock
one
How to use (GoogleMock) class layout as a template
one
Mocking googlemock file write process
one
Mock function called by aggregated object using googlemock
one
GoogleMock taunts non-virtual features
0
Failing internal calls of a function to be checked using googlemock
0
How to customize server layout using GoogleMock
0
googlemock: mocked class object cannot be created
0
googlemock: mock local object

All Articles