broun
broun

Reputation: 2593

Capture a void* argument from a mock function call

Mocking library used: GMock

Im trying to capture a void* argument passed as part of a function call on a mock object. Im able to capture int values passed through SaveArg<N> but when i try to use it to capture a void* argument it throws compilation error

Error: gmock/include/gmock/gmock-more-actions.h:155: error: ‘const void*’ is not a pointer-to-object type

Code (relevant parts):

Struct Blah
{
    int a;
    int b;
};

class SomeClass {
    void some_function(const void* arg0, u64 arg1);
};

class MockSomeClass : public SomeClass {
.
.  // Holds the mock definition
.
};

class MyClass
{
   SomeClass* _dep;

   MyClass(SomeClass* dep)
   {
       _dep = dep;
   };

   void test_function()
   {
       Blah b = new Blah();
       _dep->some_function(b, sizeof(Blah));
   }
};

TEST(SomeTestCase, One)
{
    MockSomeClass mock_object = new MockSomeClass();
    void* actual_arg;

    EXPECT_CALL(*mock_object, some_function(_,_)).WillOnce(SaveArg<0>(actual_arg)); // throws compilation err

    MyClass test_obj = new MyClass(mock_object);
    test_obj->test_function();
}

Upvotes: 0

Views: 796

Answers (1)

broun
broun

Reputation: 2593

The problem was the SaveArg expects an address location to save the contents. Since im capturing a pointer it would save the address the arg-pointer points to in the address-location i give to saveArg. So it had to be SaveArg<0>(&actual_arg). Bottom line: got confused since im capturing a pointer, though the actual_arg itself is a pointer SaveArg expects its address in memory so it can save the contents.

Upvotes: 1

Related Questions