Consider this interface:
class Interface
{
public:
virtual ~Interface = default;
virtual void open(int id) = 0;
virtual void close(int id) = 0;
protected:
// Default other special members here
};
Note that close()
may only be called after an open()
and the same ID must be used for both. Also, close()
must have been called before the next call to open()
. IDs should not repeat.
The following function uses the interface.
void actWithId(Interface & interface, int const id)
{
interface.open(id);
interface.close(id);
}
Now I got some hard coded IDs and I need to call actWithId()
for every such index.
void act(Interface & interface)
{
actWithId(interface, 1);
actWithId(interface, 2);
actWithId(interface, 3);
}
The order of the calls however is not important, so the following code would be acceptable as well:
void act(Interface & interface)
{
actWithId(interface, 2);
actWithId(interface, 3);
actWithId(interface, 1);
}
I now want to write tests with GTest/GMock in such a way that the tests are robust against a order changing refactoring. To be explicit, the tests should:
- still succeed if I changed the order of the calls to
actWithId()
- break if the
open
/close
calls are out of order. For example the following call sequence on anInterface
instance should break them:interface.open(1); interface.open(2); interface.close(1); interface.close(2);
I’m skipping the mock definition (it should be obvious) and assume a using namespace ::testing;
for the following code snippets.
To achieve requirement 2 I could use an InSequence
object like so:
MockInterface mock;
InSequence const seq;
EXPECT_CALL(mock, open(1));
EXPECT_CALL(mock, close(1));
EXPECT_CALL(mock, open(2));
EXPECT_CALL(mock, close(2));
EXPECT_CALL(mock, open(3));
EXPECT_CALL(mock, close(3));
act(mock);
But this does not fulfill requirement 1, the second implementation of act()
will break this test.
Instead I could use a Sequence
objects like so:
MockInterface mock;
Sequence const seq1, const seq2, const seq3;
EXPECT_CALL(mock, open(1)).InSequence(seq1);
EXPECT_CALL(mock, close(1)).InSequence(seq1);
EXPECT_CALL(mock, open(2)).InSequence(seq2);
EXPECT_CALL(mock, close(2)).InSequence(seq2);
EXPECT_CALL(mock, open(3)).InSequence(seq3);
EXPECT_CALL(mock, close(3)).InSequence(seq3);
act(mock);
This will accept both implementations of act()
thus fulfilling requirement 1 but it will also accept the counter-example in requirement 2. This is because I did not tell GMock that each sequence must be finished before the next is started.
The best I can come up with is introducing state to track the current id and throw if it does not match:
MockInterface mock;
std::optional<int> currentId;
EXPECT_CALL(mock, open(_)).WillRepeatedly(Invoke([&](int const id)
{
if (currentId.has_value())
{
throw std::runtime_error{ "Calling open() but is not closed" };
}
}));
EXPECT_CALL(mock, close(_)).WillRepeatedly(Invoke([&](int const id)
{
if (!currentId.has_value())
{
throw std::runtime_error{ "Calling close() but is not opened" };
}
if (*currentId != id)
{
throw std::runtime_error{ "Calling close() but was opened with a different id" };
}
}));
But in reality there are some more calls between the open()
/close()
-pairs and I would have to set up a similar action for all of them, which also kind of feels like partly re-implementing the logic. If I wanted to avoid duplicate IDs I would need to keep track of those as well which further complicated things.
Any ideas how to achieve this within the GMock framework? Ideally with one of the sequencing operations like .InSequence()
or .After()
.