Hi everyone,
our GMock tests test software components behavior over sometimes simulated several hours/days. That means lot and lots of ON_CALLs default actions to stimulate the behavior of our components.
Unfortunately the current ON_CALL implementation doesn't check whether earlier ON_CALLs are still accessible, so currently the internal lists become too long and some of our tests started to produce memory errors.
Just as it is possible to reset entire mocks via VerifyAndClear(), it would be really helpful to be able to clear ON_CALLs for specific functions.
For our current workaround I added a replace action function to the OnCallSpec:
// Replaces a set default action
OnCallSpec& ReplaceDefaultAction(const Action<F>& action) {
ExpectSpecProperty(last_clause_ == kWillByDefault,
".WillByDefault() must be set before replaced.");
ExpectSpecProperty(!action.IsDoDefault(),
"DoDefault() cannot be used in ON_CALL().");
action_ = action;
return *this;
}
which I can use as:
typedef std::remove_reference<decltype(ON_CALL(*_mock, AFunction(_)))>::type AFunction_OnCallType;
static AFunction_OnCallType* AFunction_OnCallPtr = 0;
if(!_initialized)
{
AFunction_OnCallPtr = &ON_CALL(*_mock, AFunction(_)).WillByDefault(DoAll(SetArgPointee<0>(newValue), Return(OK)));
EXPECT_CALL(*_mock, AFunction(_)).Times(AnyNumber());
}
else
{
AFunction_OnCallPtr->ZSG_ReplaceDefaultAction(DoAll(SetArgPointee<0>(newValue), Return(OK)));
}
so we at least have the option to replace all the default actions. I would very much like to use a standard method instead though, and if possible without having to keep a reference to the OnCallSpec.