Replacing an ON_CALL default action

1,056 views
Skip to first unread message

Troels

unread,
Jun 16, 2014, 10:30:51 AM6/16/14
to googl...@googlegroups.com
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.
 

Uri Goren

unread,
Feb 22, 2016, 10:34:07 AM2/22/16
to Google C++ Mocking Framework
Hi,

I also want the exact same thing, though for a somewhat different reason.

I use saveArg to store to a local variable:
void foo() {
    int x;
    ON_CALL(mock, bar(_)).WillByDefault(SaveArg<0>(&x);
    something();
}

After foo() returns, ON_CALL is still active and a call to bar() would corrupt the stack.
I can use ON_CALL again, with another action. But as long as I don't delete the first one, there's still a dangling pointer out there.

So a way to reset the action would be most welcome.

Thanks,
Uri Goren.
Reply all
Reply to author
Forward
0 new messages