Question

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();
}
Was it helpful?

Solution

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top