I'm using googlemock/test first time. Compilation error comes due to following line:
EXPECT_CALL(orderWriter, writeOrders(aNonZeroSizeOrder))
.Times(1);
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>'
/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);
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:
operator ==
to your value class::testing::Field
- or just ::testing::_
to see that I am right.Full list of available matchers are here: CheatSheet.md#matchers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With