Google Mock giving compile error when attempting to specify a return value
I'm using Google Test and Google Mock for my C++/Qt application. I've been having great success with this setup until just now when I tried this:
QList<AbstractSurface::VertexRow> rowList;
for (unsigned i = 0; i < rows; ++i)
{
AbstractSurface::VertexRow curRow(new AbstractSurface::Vertex[cols]);
for (unsigned j = 0; j < cols; ++j)
{
curRow[j] = AbstractSurface::Vertex();
}
rowList.append(curRow);
}
ON_CALL(surface, numRows_impl()).WillByDefault(Return(rows));
ON_CALL(surface, numColumns_impl()).WillByDe开发者_StackOverflowfault(Return(cols));
ON_CALL(surface, popAllRows_impl()).WillOnce(Return(rowList));
/* ... */
Attempting to compile this results in the following error message from gcc:
../../3DWaveSurface/test/TestWaterfallPresenter.cc: In member function ‘virtual void<unnamed>::WaterfallPresenterTest_CallingPaintGLCallsPaintRowForEachRowInSurface_Test::TestBody()’:
../../3DWaveSurface/test/TestWaterfallPresenter.cc:140:41: error: ‘class testing::internal::OnCallSpec<QList<boost::shared_array<AbstractSurface::Vertex> >()>’ has no member named ‘WillOnce’
If it helps, VertexRow
is a typedef
for a boost::shared_array<Vertex>
and Vertex
is a struct
with a valid empty constructor.
Is this an error in what I wrote for the test or some incompatibility with using QList
or shared_array
?
SOLUTION After following VJo's recommendation, my tests compiled and ran but then crashed:
Stack trace:
: Failure
Uninteresting mock function call - returning default value.
Function call: popAllRows_impl()
The mock function has no default action set, and its return type has no default value set.
The process "/home/corey/development/3DWaveSurface-build-desktop-debug/test/test" crashed.
Because there was no default return for popAllRows_impl()
. I added a default:
ON_CALL(surface, popAllRows_impl()).WillByDefault(Return(QList<AbstractSurface::VertexRow>()));
To my SetUp()
and all is well. As VJo pointed out, there is no WillOnce() for ON_CALL
but there is for EXPECT_CALL
and I missed this in the cook book..
The simplest solution is :
EXPECT_CALL(surface, popAllRows_impl()).WillOnce(Return(rowList));
EDIT :
ON_CALL
has WillByDefault
, but no WillOnce
method. Check the gmock cookbook
精彩评论