Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arbitrary arguments to invocked methods with Google C++ Mocking Framework (Google Mock) (V1.5)

I have a mock method. When it is called, I'd like it to call another function before calling its normal behavior. Something like :

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
    .WillOnce(DoAll(
        Invoke(my_obj, &SomeAdditionalMethodIWantToCall),
        Invoke(my_obj, &DefaultBehavior),
        ));

The only problem is that SomeAdditionalMethodIWantToCall expects parameter that not at all related to the one provided to MockedMethod. I'd like to be able to give them but I am struggling with the syntax. I wish there was something like (in fake syntax) :

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
    .WillOnce(DoAll(
        Invoke(my_obj, &SomeAdditionalMethodIWantToCall, arg1, arg2, arg3),
        Invoke(my_obj, &DefaultBehavior),
        ));

I have looked for such a thing in the documentation without any success.

In Using a Function or a Functor as an Action, we have :

  • Invoke(f), Invoke(object_pointer, &class::method), InvokeWithoutArgs(f), InvokeWithoutArgs(object_pointer, &class::method) which will just forward (or not) the parameter provided to the mocked function when it is called.

  • InvokeArgument<N>(arg1, arg2, ..., argk) seems to be for calling one of the parameter.

In Composite Actions

  • WithArg<N>(a) and WithArgs<N1, N2, ..., Nk>(a) seem to be to select which parameters from the original function get forwarded.

I guess I am missing something quite obvious but I am a bit stuck here so any suggestion will help.

like image 833
SylvainD Avatar asked Jan 15 '15 12:01

SylvainD


People also ask

How do you write a mock function in Gtest?

You should create a wrapper for the function that you want to mock. Here is an example. First you create an interface for the wrapper, then you create two inherited versions. One for production code, which calls the original wfdEnumerateDevices function, and one for testing, which is used for mocking that function.

What is difference between On_call and Expect_call?

So use ON_CALL by default, and only use EXPECT_CALL when you actually intend to verify that the call is made.

How do you mock in Gmock?

Using the Turtle interface as example, here are the simple steps you need to follow: Derive a class MockTurtle from Turtle . Take a virtual function of Turtle (while it's possible to mock non-virtual methods using templates, it's much more involved). In the public: section of the child class, write MOCK_METHOD();

What is Googlemock?

Gmock is a mocking framework for the Groovy language. Gmock is all about simple syntax and readability of your tests so you spend less time learning the framework and more writing code. To use Gmock just drop the gmock jar file in your classpath.


1 Answers

If you are using c++11, you can use lambda-functions like this:

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
.WillOnce(DoAll(
    InvokeWithoutArgs([&]() { 
        my_obj->SomeAdditionalMethodIWantToCall(arg1, arg2, arg3); 
    },
    Invoke(my_obj, &DefaultBehavior)   
));
like image 143
Myon Avatar answered Oct 05 '22 17:10

Myon