Googlemock EXPECT_CALL失败,并出现错误:“ operator ==”不匹配(操作数类型为“ const XX”

时间:2019-06-05 05:57:21

标签: googletest googlemock

我是第一次使用googlemock / test。编译错误来自以下行:

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

错误:

错误:'operator =='操作数类型不匹配的是'const Order'和'const Order' 不是从'const std :: istream_iterator <_Tp,_CharT,_Traits,_Dist>'

派生的

注意:候选者:模板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:注意:“ const Order”不是从“ const std :: pair”派生的<_T1,_T2>'       如果(!(* __ first1 == * __ first2))            ~~~~~~~~~~~ ^ ~~~~~~~~~~~

如果我发表评论,那么编译成功。 在使用const ref参数定义Mock方法时,我做错什么了吗?


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);

1 个答案:

答案 0 :(得分:1)

当您写这篇文章时:


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

Googlemock了解这一点:


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

这是因为在EXPECT_CALL()的参数中googlemock需要匹配器,而不是值。当您提供简单值时-它们会“无声地”转换为::testing::Eq(value)返回的匹配器。

此类Eq匹配器是通过operator==实现的-因此出现了错误。

您有2种解决方法:

  1. operator ==添加到您的值类别中
  2. 使用其他匹配器-例如::testing::Field-或仅使用::testing::_来确定我是对的。

可用的匹配器的完整列表在这里:CheatSheet.md#matchers