Invoking function in google mock with abstract base class reference gives num arguments error
Asked Answered
B

3

6

I have an issue with the google mock EXPECT_CALL macro. The following code gives compilation error on the EXPECT_CALL Statement:

error C2660: 'testing::Eq' : function does not take 1 arguments \gmock-1.6.0\include\gmock\gmock-matchers.h

Basically I have a base container and base data object for that container, both abstract and a cache which has a pointer to base container and an Add method that takes a reference to base data object. I have created a basic program to demonstrate the issue. Thanks a lot if anyone can help.

#include "gtest/gtest.h"
#include "gmock/gmock.h"

namespace
{
class BaseData
{
    public:
    virtual void SetValue(const int value) = 0;
};

class BaseContainer
{ 
    public:
    virtual void Add(const BaseData& data) = 0;
};



class MockContainer : public BaseContainer
{
public:
    MOCK_METHOD1(Add, void (const BaseData& data));
};

class MockData : public BaseData
{
public:
    MOCK_METHOD1(SetValue, void (int));
};

class Cache
{
    private:
    BaseContainer* container;
    public:
    Cache(BaseContainer* c)
    {
        container = c;
    }
    ~Cache()
    {
    }

    void AddToContainer(const BaseData& data)
    {
        container->Add(data);
    }
};

class CacheTestFixture : public ::testing::Test
{
protected:
    CacheTestFixture() {}

    virtual ~CacheTestFixture() {}

    virtual void SetUp() {}

    virtual void TearDown() {}

};

TEST_F(CacheTestFixture, TestAdd)
{
    MockData data;
    MockContainer container;
    EXPECT_CALL(container, Add(data)).WillRepeatedly(::testing::Return());
    Cache c(&container);
    ASSERT_NO_THROW(c.AddToContainer(data));
}
}

int _tmain(int argc, _TCHAR* argv[])
{
::testing::InitGoogleMock(&argc, argv);

return RUN_ALL_TESTS();
}
Bawbee answered 17/9, 2013 at 3:54 Comment(1)
Just a question - would the MOCK_METHOD for SetValue require const int instead of just int?Pickaninny
B
12
EXPECT_CALL(container, Add(testing::Ref(data))).WillRepeatedly(::testing::Return());

To send the mock implementation as a base class reference, testing::Eq would require the == operator to be implemented on the abstract base class which is not desirable.

Bawbee answered 18/9, 2013 at 15:6 Comment(0)
B
2

You need to use ::testing::Eq(ByRef(data)) ::testing::Eqis Matcher that needs to be used, read about matchers on Google Mock Cookbook.

Bane answered 17/9, 2013 at 19:18 Comment(1)
Tried that also, that does not fix the issue. If I do that I still get error: 'bool testing::internal::operator ==(T *,const testing::internal::linked_ptr<T> &)' : could not deduce template argument for 'T *' from 'const BaseData'Bawbee
B
0

The answer from Blackhole is correct, you have to use

::testing::Eq(ByRef(data))

But in order for a correct copmarison you have to define the operator== for your BaseData since it is an abstract class. You can do this in your testing code as a free function:

bool operator==(const BaseData& left, const BaseData& right)
{
    return ...;
}

I once asked a similar question, see here

Bark answered 18/12, 2017 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.