Actions Reference

    Side Effects

    ActionDescription
    Assign(&variable, value)Assign value to variable.
    DeleteArg<N>()Delete the N-th (0-based) argument, which must be a pointer.
    SaveArg<N>(pointer)Save the N-th (0-based) argument to pointer.
    SaveArgPointee<N>(pointer)Save the value pointed to by the N-th (0-based) argument to pointer.
    SetArgReferee<N>(value)Assign value to the variable referenced by the N-th (0-based) argument.
    Assign value to the variable pointed by the N-th (0-based) argument.
    SetArgumentPointee<N>(value)Same as SetArgPointee<N>(value). Deprecated. Will be removed in v1.7.0.
    SetArrayArgument<N>(first, last)Copies the elements in source range [first, last) to the array pointed to by the N-th (0-based) argument, which can be either a pointer or an iterator. The action does not take ownership of the elements in the source range.
    SetErrnoAndReturn(error, value)Set errno to error and return value.
    Throw(exception)Throws the given exception, which can be any copyable value. Available since v1.1.0.

    In the following, by “callable” we mean a free function, std::function, functor, or lambda.

    The return value of the invoked function is used as the return value of the action.

    Invoke(callback) and InvokeWithoutArgs(callback) take ownership of , which must be permanent. The type of callback must be a base callback type instead of a derived one, e.g.

    In InvokeArgument<N>(...), if an argument needs to be passed by reference, wrap it inside std::ref(). For example,

    Default Action

    ActionDescription
    DoDefault()Do the default action (specified by ON_CALL() or the built-in one).

    Note: due to technical reasons, DoDefault() cannot be used inside a composite action - trying to do so will result in a run-time error.

    Defining Actions

    MacroDescription
    ACTION(Sum) { return arg0 + arg1; }Defines an action Sum() to return the sum of the mock function’s argument #0 and #1.
    ACTION_P(Plus, n) { return arg0 + n; }Defines an action Plus(n) to return the sum of the mock function’s argument #0 and n.
    ACTION_Pk(Foo, p1, …, pk) { statements; }Defines a parameterized action Foo(p1, …, pk) to execute the given statements.

    The macros cannot be used inside a function or class.