Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Googlemock EXPECT_CALL fails with error: no match for 'operator==' (operand types are 'const XX'

I'm using googlemock/test first time. Compilation error comes due to following line:

EXPECT_CALL(orderWriter, writeOrders(aNonZeroSizeOrder))
    .Times(1);

ERROR:

error: no match for 'operator==' operand types are 'const Order' and 'const Order' 'const Order' is not derived from 'const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>'

note: candidate: template bool std::operator==(const std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)

/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:800:22: note: 'const Order' is not derived from 'const std::pair<_T1, _T2>' if (!(*__first1 == *__first2)) ~~~~~~~~~~~^~~~~~~~~~~~~

If I comment it, compilation is successful. Am I doing something wrong in defining Mock method with const ref parameter?


class Order
{
    //represents an order
};

class OrderWriter
{
public:
    virtual void writeOrders(const std::vector<Order>& allOrders)
    {
        // ... writes the orders somewhere ...
    }
};

class OrderStore
{
public:
    virtual std::vector<Order> getOrders() const
    {
        // ... returns an array of orders ...
    }
};

class SimpleOrderManager
{
public:
    void writeAllOrders(OrderStore& orderStore, OrderWriter& orderWriter)
    {
        std::vector<Order> allOrders = orderStore.getOrders();

        if(allOrders.size() == 0)
            throw "No orders in store";
        orderWriter.writeOrders(allOrders);
    }
};

/////////////////////////////////////////////////////////////////////////////////////
//Need to mock class OrderStore as we need to return vector
//without changing original class OrderStore which is not implemented (thus not returning vector)
class MockOrderStore : public OrderStore
{
public:
    MOCK_CONST_METHOD0(getOrders, std::vector<Order> () );
};

//To check if OrderWriter::writeOrders() is called
class MockOrderWriter : public OrderWriter
{
public:
    MOCK_METHOD1(writeOrders, void (const std::vector<Order>& ) );
};

TEST(SimpleOrderManager_TestCase,  expect_call_writeOrders )
{
    std::vector<Order> aNonZeroSizeOrder{Order()};
    MockOrderStore  aMockOrderStore;
    MockOrderWriter aMockOrderWriter;
    SimpleOrderManager aSMO;
    //Call MockOrderStore::getOrders() not OrderStore::getOrders()
    //as we need to return empty vector without changing original class OrderStore
    EXPECT_CALL(aMockOrderStore, getOrders)
    .Times(1)
    .WillOnce(Return(aNonZeroSizeOrder));

/*i**************ERROR in this line **************************/
    EXPECT_CALL(aMockOrderWriter, writeOrders(aNonZeroSizeOrder)).Times(1);

    aSMO.writeAllOrders(aMockOrderStore, aMockOrderWriter);
}```

EDIT: Problems solved when I pass "_" instead of parameter as suggested by @PiotrNycz
It works when I change like this:
EXPECT_CALL(aMockOrderWriter, writeOrders(_)).Times(1);
like image 866
sheikhazad Avatar asked Sep 19 '25 16:09

sheikhazad


1 Answers

When you write this:


EXPECT_CALL(orderWriter, writeOrders(aNonZeroSizeOrder))
    .Times(1);

Googlemock understands this:


EXPECT_CALL(orderWriter, writeOrders(::testing::Eq(aNonZeroSizeOrder)))
//                                   ^^^^^^^^^^^^^^
    .Times(1);

This is because googlemock in arguments to EXPECT_CALL() expects matchers - not values. When you provide simple values - they are "silently" converted to matcher returned by ::testing::Eq(value).

Such Eq matcher is implemented with operator== - so the error you have.

You have 2 ways to solve it:

  1. add operator == to your value class
  2. use other matcher - like ::testing::Field - or just ::testing::_ to see that I am right.

Full list of available matchers are here: CheatSheet.md#matchers

like image 108
PiotrNycz Avatar answered Sep 22 '25 03:09

PiotrNycz