Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 633
Sahil Avatar asked Sep 17 '13 03:09

Sahil


2 Answers

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.

like image 123
Sahil Avatar answered Sep 24 '22 14:09

Sahil


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

like image 25
Blackhole Avatar answered Sep 24 '22 14:09

Blackhole