Here is my situation with testing. There is a note in googletest that EXPECT_CALL’s must be set first before the calls on mock methods (https://google.github.io/googletest/gmock_for_dummies.html#expectation-ordering). My problem is that I have a mock object that will only be initialized later. Right after it will be initialized a mock method from the mock object will be called right away.
With that, since I can’t set an EXPECT_CALL on a not yet existing mock object. I am forced to set an EXPECT_CALL just in time it the mock object is created. What happens in my tests now is that the mock method call happens first before the EXPECT_CALL. Because of that sequence my test fails because it did not catch the mock method that was expected in the EXPECT_CALL.
Here it is in pseudo code:
In code I am testing.
Client client = new Client();
// some actions with client…
// Later on Document object is created…
std::unique<Document> doc = client->CreateDocument();
if (doc->Process())
{
….
In googletest.
TEST_F(ClientTesting, CanProcess)
{
… expect calls on client …
// Only being able to add expect call here because before this, document object is not yet created.
EXPECT_CALL(*mockDoc, Process).WillOnce(Return(true));
}
With that the test shows that there was an uninteresting mock method call on Document object’s Process method. Then it would fail saying that an expect call for Document object’s Process method was expected to be called but not called. I believe this due to the mock method being called first in the code under test before the EXPECT_CALL on it in the unit test.
I tried adding some sleep()
after creation of Document
object to see if the proceeding mock method calls on it would be captured by EXCEPT_CALL.
Client client = new Client();
// some actions with client…
// Later on Document object is created…
std::unique<Document> doc = client->CreateDocument();
sleep(2);
if (doc->Process())
{
….
2
Easiest solution possible: put EXPECT_CALL
in mock’s constructor:
https://godbolt.org/z/5YhfP9K6x
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace testing;
struct Doc
{
Doc()
{ EXPECT_CALL(*this, process).WillOnce(Return(true)); }
MOCK_METHOD(bool, process,());
};
auto f()
{
return std::make_unique<Doc>();
}
bool g()
{
return f()->process();
}
TEST(a,b)
{
EXPECT_TRUE(g());
}
Or for a bit more control, you can wrap it in a class that controls its return value:
https://godbolt.org/z/5q8e6e846
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace testing;
struct IDoc
{
virtual bool process() = 0;
virtual ~IDoc() = default;
};
struct DocMock : IDoc
{
MOCK_METHOD(bool, process,(), (override));
};
struct DocWrap : IDoc
{
static inline bool retval = false;
bool process() override
{
EXPECT_CALL(mock, process).WillOnce(Return(retval));
return mock.process();
}
DocMock mock{};
};
std::unique_ptr<IDoc> f()
{
return std::make_unique<DocWrap>();
}
bool g()
{
return f()->process();
}
TEST(a,b)
{
DocWrap::retval=false;
EXPECT_FALSE(g());
}
If handling that via static variable is a good idea is another story.