1*28f6c2f2SEnji Cooper# Actions Reference 2*28f6c2f2SEnji Cooper 3*28f6c2f2SEnji Cooper[**Actions**](../gmock_for_dummies.md#actions-what-should-it-do) specify what a 4*28f6c2f2SEnji Coopermock function should do when invoked. This page lists the built-in actions 5*28f6c2f2SEnji Cooperprovided by GoogleTest. All actions are defined in the `::testing` namespace. 6*28f6c2f2SEnji Cooper 7*28f6c2f2SEnji Cooper## Returning a Value 8*28f6c2f2SEnji Cooper 9*28f6c2f2SEnji Cooper| Action | Description | 10*28f6c2f2SEnji Cooper| :-------------------------------- | :-------------------------------------------- | 11*28f6c2f2SEnji Cooper| `Return()` | Return from a `void` mock function. | 12*28f6c2f2SEnji Cooper| `Return(value)` | Return `value`. If the type of `value` is different to the mock function's return type, `value` is converted to the latter type <i>at the time the expectation is set</i>, not when the action is executed. | 13*28f6c2f2SEnji Cooper| `ReturnArg<N>()` | Return the `N`-th (0-based) argument. | 14*28f6c2f2SEnji Cooper| `ReturnNew<T>(a1, ..., ak)` | Return `new T(a1, ..., ak)`; a different object is created each time. | 15*28f6c2f2SEnji Cooper| `ReturnNull()` | Return a null pointer. | 16*28f6c2f2SEnji Cooper| `ReturnPointee(ptr)` | Return the value pointed to by `ptr`. | 17*28f6c2f2SEnji Cooper| `ReturnRef(variable)` | Return a reference to `variable`. | 18*28f6c2f2SEnji Cooper| `ReturnRefOfCopy(value)` | Return a reference to a copy of `value`; the copy lives as long as the action. | 19*28f6c2f2SEnji Cooper| `ReturnRoundRobin({a1, ..., ak})` | Each call will return the next `ai` in the list, starting at the beginning when the end of the list is reached. | 20*28f6c2f2SEnji Cooper 21*28f6c2f2SEnji Cooper## Side Effects 22*28f6c2f2SEnji Cooper 23*28f6c2f2SEnji Cooper| Action | Description | 24*28f6c2f2SEnji Cooper| :--------------------------------- | :-------------------------------------- | 25*28f6c2f2SEnji Cooper| `Assign(&variable, value)` | Assign `value` to variable. | 26*28f6c2f2SEnji Cooper| `DeleteArg<N>()` | Delete the `N`-th (0-based) argument, which must be a pointer. | 27*28f6c2f2SEnji Cooper| `SaveArg<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer`. | 28*28f6c2f2SEnji Cooper| `SaveArgPointee<N>(pointer)` | Save the value pointed to by the `N`-th (0-based) argument to `*pointer`. | 29*28f6c2f2SEnji Cooper| `SetArgReferee<N>(value)` | Assign `value` to the variable referenced by the `N`-th (0-based) argument. | 30*28f6c2f2SEnji Cooper| `SetArgPointee<N>(value)` | Assign `value` to the variable pointed by the `N`-th (0-based) argument. | 31*28f6c2f2SEnji Cooper| `SetArgumentPointee<N>(value)` | Same as `SetArgPointee<N>(value)`. Deprecated. Will be removed in v1.7.0. | 32*28f6c2f2SEnji Cooper| `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. | 33*28f6c2f2SEnji Cooper| `SetErrnoAndReturn(error, value)` | Set `errno` to `error` and return `value`. | 34*28f6c2f2SEnji Cooper| `Throw(exception)` | Throws the given exception, which can be any copyable value. Available since v1.1.0. | 35*28f6c2f2SEnji Cooper 36*28f6c2f2SEnji Cooper## Using a Function, Functor, or Lambda as an Action 37*28f6c2f2SEnji Cooper 38*28f6c2f2SEnji CooperIn the following, by "callable" we mean a free function, `std::function`, 39*28f6c2f2SEnji Cooperfunctor, or lambda. 40*28f6c2f2SEnji Cooper 41*28f6c2f2SEnji Cooper| Action | Description | 42*28f6c2f2SEnji Cooper| :---------------------------------- | :------------------------------------- | 43*28f6c2f2SEnji Cooper| `f` | Invoke `f` with the arguments passed to the mock function, where `f` is a callable. | 44*28f6c2f2SEnji Cooper| `Invoke(f)` | Invoke `f` with the arguments passed to the mock function, where `f` can be a global/static function or a functor. | 45*28f6c2f2SEnji Cooper| `Invoke(object_pointer, &class::method)` | Invoke the method on the object with the arguments passed to the mock function. | 46*28f6c2f2SEnji Cooper| `InvokeWithoutArgs(f)` | Invoke `f`, which can be a global/static function or a functor. `f` must take no arguments. | 47*28f6c2f2SEnji Cooper| `InvokeWithoutArgs(object_pointer, &class::method)` | Invoke the method on the object, which takes no arguments. | 48*28f6c2f2SEnji Cooper| `InvokeArgument<N>(arg1, arg2, ..., argk)` | Invoke the mock function's `N`-th (0-based) argument, which must be a function or a functor, with the `k` arguments. | 49*28f6c2f2SEnji Cooper 50*28f6c2f2SEnji CooperThe return value of the invoked function is used as the return value of the 51*28f6c2f2SEnji Cooperaction. 52*28f6c2f2SEnji Cooper 53*28f6c2f2SEnji CooperWhen defining a callable to be used with `Invoke*()`, you can declare any unused 54*28f6c2f2SEnji Cooperparameters as `Unused`: 55*28f6c2f2SEnji Cooper 56*28f6c2f2SEnji Cooper```cpp 57*28f6c2f2SEnji Cooperusing ::testing::Invoke; 58*28f6c2f2SEnji Cooperdouble Distance(Unused, double x, double y) { return sqrt(x*x + y*y); } 59*28f6c2f2SEnji Cooper... 60*28f6c2f2SEnji CooperEXPECT_CALL(mock, Foo("Hi", _, _)).WillOnce(Invoke(Distance)); 61*28f6c2f2SEnji Cooper``` 62*28f6c2f2SEnji Cooper 63*28f6c2f2SEnji Cooper`Invoke(callback)` and `InvokeWithoutArgs(callback)` take ownership of 64*28f6c2f2SEnji Cooper`callback`, which must be permanent. The type of `callback` must be a base 65*28f6c2f2SEnji Coopercallback type instead of a derived one, e.g. 66*28f6c2f2SEnji Cooper 67*28f6c2f2SEnji Cooper```cpp 68*28f6c2f2SEnji Cooper BlockingClosure* done = new BlockingClosure; 69*28f6c2f2SEnji Cooper ... Invoke(done) ...; // This won't compile! 70*28f6c2f2SEnji Cooper 71*28f6c2f2SEnji Cooper Closure* done2 = new BlockingClosure; 72*28f6c2f2SEnji Cooper ... Invoke(done2) ...; // This works. 73*28f6c2f2SEnji Cooper``` 74*28f6c2f2SEnji Cooper 75*28f6c2f2SEnji CooperIn `InvokeArgument<N>(...)`, if an argument needs to be passed by reference, 76*28f6c2f2SEnji Cooperwrap it inside `std::ref()`. For example, 77*28f6c2f2SEnji Cooper 78*28f6c2f2SEnji Cooper```cpp 79*28f6c2f2SEnji Cooperusing ::testing::InvokeArgument; 80*28f6c2f2SEnji Cooper... 81*28f6c2f2SEnji CooperInvokeArgument<2>(5, string("Hi"), std::ref(foo)) 82*28f6c2f2SEnji Cooper``` 83*28f6c2f2SEnji Cooper 84*28f6c2f2SEnji Coopercalls the mock function's #2 argument, passing to it `5` and `string("Hi")` by 85*28f6c2f2SEnji Coopervalue, and `foo` by reference. 86*28f6c2f2SEnji Cooper 87*28f6c2f2SEnji Cooper## Default Action 88*28f6c2f2SEnji Cooper 89*28f6c2f2SEnji Cooper| Action | Description | 90*28f6c2f2SEnji Cooper| :------------ | :----------------------------------------------------- | 91*28f6c2f2SEnji Cooper| `DoDefault()` | Do the default action (specified by `ON_CALL()` or the built-in one). | 92*28f6c2f2SEnji Cooper 93*28f6c2f2SEnji Cooper{: .callout .note} 94*28f6c2f2SEnji Cooper**Note:** due to technical reasons, `DoDefault()` cannot be used inside a 95*28f6c2f2SEnji Coopercomposite action - trying to do so will result in a run-time error. 96*28f6c2f2SEnji Cooper 97*28f6c2f2SEnji Cooper## Composite Actions 98*28f6c2f2SEnji Cooper 99*28f6c2f2SEnji Cooper| Action | Description | 100*28f6c2f2SEnji Cooper| :----------------------------- | :------------------------------------------ | 101*28f6c2f2SEnji Cooper| `DoAll(a1, a2, ..., an)` | Do all actions `a1` to `an` and return the result of `an` in each invocation. The first `n - 1` sub-actions must return void and will receive a readonly view of the arguments. | 102*28f6c2f2SEnji Cooper| `IgnoreResult(a)` | Perform action `a` and ignore its result. `a` must not return void. | 103*28f6c2f2SEnji Cooper| `WithArg<N>(a)` | Pass the `N`-th (0-based) argument of the mock function to action `a` and perform it. | 104*28f6c2f2SEnji Cooper| `WithArgs<N1, N2, ..., Nk>(a)` | Pass the selected (0-based) arguments of the mock function to action `a` and perform it. | 105*28f6c2f2SEnji Cooper| `WithoutArgs(a)` | Perform action `a` without any arguments. | 106*28f6c2f2SEnji Cooper 107*28f6c2f2SEnji Cooper## Defining Actions 108*28f6c2f2SEnji Cooper 109*28f6c2f2SEnji Cooper| Macro | Description | 110*28f6c2f2SEnji Cooper| :--------------------------------- | :-------------------------------------- | 111*28f6c2f2SEnji Cooper| `ACTION(Sum) { return arg0 + arg1; }` | Defines an action `Sum()` to return the sum of the mock function's argument #0 and #1. | 112*28f6c2f2SEnji Cooper| `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`. | 113*28f6c2f2SEnji Cooper| `ACTION_Pk(Foo, p1, ..., pk) { statements; }` | Defines a parameterized action `Foo(p1, ..., pk)` to execute the given `statements`. | 114*28f6c2f2SEnji Cooper 115*28f6c2f2SEnji CooperThe `ACTION*` macros cannot be used inside a function or class. 116