| 1 |
--- |
| 2 |
title: 'Mocks: Integrating with `testthat`' |
| 3 |
author: "Lukasz A. Bartnik" |
| 4 |
date: "`r Sys.Date()`" |
| 5 |
output: rmarkdown::html_vignette |
| 6 |
vignette: > |
| 7 |
%\VignetteIndexEntry{Mocks: Integrating with testthat} |
| 8 |
%\VignetteEngine{knitr::rmarkdown} |
| 9 |
%\VignetteEncoding{UTF-8} |
| 10 |
--- |
| 11 |
|
| 12 |
```{r include=FALSE} |
| 13 |
library(mockery) |
| 14 |
|
| 15 |
library(knitr) |
| 16 |
knitr::opts_chunk$set(collapse = TRUE, comment = "#>") |
| 17 |
summary <- NULL |
| 18 |
|
| 19 |
# pretend we're testing a package |
| 20 |
Sys.setenv(TESTTHAT = "true") |
| 21 |
Sys.setenv(TESTTHAT_PKG = "mockery") |
| 22 |
``` |
| 23 |
|
| 24 |
Mock object, which is a part of the `mockery` package, started as an |
| 25 |
extension to [testthat](https://github.com/r-lib/testthat)'s `with_mocked_bindings()` |
| 26 |
facility. Its main purpose was to simplify the replacement (mocking) |
| 27 |
of a given function by means of `with_mocked_bindings` and the later |
| 28 |
verification of actual calls invoked on the replacing function. |
| 29 |
|
| 30 |
The `mockery` package which provides its own stubbing facility, the |
| 31 |
`stub()` function. Here, however, we will look only at how `mock()` |
| 32 |
can be used together with `with_mocked_bindings()`. |
| 33 |
|
| 34 |
|
| 35 |
## Mocks |
| 36 |
|
| 37 |
### Creating a `mock` function |
| 38 |
|
| 39 |
Mocking is a well-known technique when it comes to unit-testing and in |
| 40 |
most languages there is some notion of a mock object. In R, however, the |
| 41 |
natural equivalent of a mock object is a mock _function_ - and this is |
| 42 |
exactly what a call to `mock()` will produce. |
| 43 |
|
| 44 |
```{r create_mock} |
| 45 |
m <- mock() |
| 46 |
``` |
| 47 |
|
| 48 |
|
| 49 |
### Return values |
| 50 |
|
| 51 |
Let's look at arguments accepted by the `mock()` _factory_ function. The |
| 52 |
main is a list of values which will be returned upon subsequent calls to |
| 53 |
`m`. |
| 54 |
|
| 55 |
```{r return_values} |
| 56 |
m <- mock(1, 2, 3) |
| 57 |
m() |
| 58 |
m() |
| 59 |
m() |
| 60 |
``` |
| 61 |
|
| 62 |
`mock()` can take also an expression which will be evaluated upon a call. |
| 63 |
|
| 64 |
```{r return_expression} |
| 65 |
x <- 1 |
| 66 |
y <- 2 |
| 67 |
m <- mock(x + y) |
| 68 |
m() |
| 69 |
``` |
| 70 |
|
| 71 |
|
| 72 |
### Cycling through return values |
| 73 |
|
| 74 |
By default, if the total number of calls exceeds the number of defined |
| 75 |
return values, the _mock_ function will throw an exception. However, one |
| 76 |
can also choose to cycle through the list of retun values by setting the |
| 77 |
`cycle` argument of `mock()` to `TRUE`. |
| 78 |
|
| 79 |
```{r cycle_no} |
| 80 |
#| error: true |
| 81 |
m <- mock(1, 2) |
| 82 |
m() |
| 83 |
m() |
| 84 |
m() |
| 85 |
``` |
| 86 |
|
| 87 |
```{r cycle_true} |
| 88 |
m <- mock(1, 2, cycle = TRUE) |
| 89 |
m() |
| 90 |
m() |
| 91 |
m() |
| 92 |
m() |
| 93 |
``` |
| 94 |
|
| 95 |
|
| 96 |
If a return value is defined by an expression, this expression will be |
| 97 |
evaluated each time a cycle reaches its position. |
| 98 |
|
| 99 |
```{r cycle_expression} |
| 100 |
x <- 1 |
| 101 |
y <- 2 |
| 102 |
m <- mock(1, x + y, cycle = TRUE) |
| 103 |
|
| 104 |
m() |
| 105 |
m() |
| 106 |
``` |
| 107 |
```{r cycle_expression_2nd} |
| 108 |
y <- 10 |
| 109 |
m() |
| 110 |
m() |
| 111 |
``` |
| 112 |
|
| 113 |
|
| 114 |
### Evaluating expression in an environment of choice |
| 115 |
|
| 116 |
Finally, one can specify the environment where the return expression is |
| 117 |
evaluated. |
| 118 |
|
| 119 |
```{r return_expression_env} |
| 120 |
x <- 1 |
| 121 |
y <- 2 |
| 122 |
e <- new.env() |
| 123 |
m <- mock(x + y, envir = e, cycle = TRUE) |
| 124 |
|
| 125 |
m() |
| 126 |
e$x <- 10 |
| 127 |
m() |
| 128 |
``` |
| 129 |
|
| 130 |
|
| 131 |
## Integration with `with_mocked_bindings()` |
| 132 |
|
| 133 |
### Simple integration |
| 134 |
|
| 135 |
Using mock functions with `testthat`'s `with_mocked_bindings()` is pretty |
| 136 |
straightforward. |
| 137 |
|
| 138 |
```{r with_mock, message=FALSE} |
| 139 |
library(testthat) |
| 140 |
|
| 141 |
m <- mock(1) |
| 142 |
f <- function (x) summary(x) |
| 143 |
with_mocked_bindings(f = m, { |
| 144 |
expect_equal(f(iris), 1) |
| 145 |
}) |
| 146 |
``` |
| 147 |
|
| 148 |
|
| 149 |
### Verifying the number of calls |
| 150 |
|
| 151 |
The `mockery` package comes with a few additional expectations which |
| 152 |
might turn out to be a very useful extension to `testthat`'s API. One |
| 153 |
can for example verify the number and signature of calls invoked on a |
| 154 |
mock function, as well as the values of arguments passed in those calls. |
| 155 |
|
| 156 |
First, let's make sure the mocked function is called exactly as many |
| 157 |
times as we expect. This can be done with `expect_called()`. |
| 158 |
|
| 159 |
```{r expect_called} |
| 160 |
m <- mock(1, 2) |
| 161 |
|
| 162 |
m() |
| 163 |
expect_called(m, 1) |
| 164 |
|
| 165 |
m() |
| 166 |
expect_called(m, 2) |
| 167 |
``` |
| 168 |
|
| 169 |
And here is what happens when we get the number of calls wrong. |
| 170 |
|
| 171 |
```{r expect_called_error} |
| 172 |
#| error: true |
| 173 |
expect_called(m, 1) |
| 174 |
expect_called(m, 3) |
| 175 |
``` |
| 176 |
|
| 177 |
### Verify the call signature |
| 178 |
|
| 179 |
Another new expectation is `expect_call()` which compares the signature |
| 180 |
of the actual call as invoked on the mock function with the expected one. |
| 181 |
It takes as arguments: the mock function, the call number, expected call. |
| 182 |
|
| 183 |
```{r expect_call} |
| 184 |
m <- mock(1) |
| 185 |
with_mocked_bindings(summary = m, { |
| 186 |
summary(iris) |
| 187 |
}) |
| 188 |
|
| 189 |
expect_call(m, 1, summary(iris)) |
| 190 |
``` |
| 191 |
|
| 192 |
And here is what happens if the call doesn't match. |
| 193 |
|
| 194 |
```{r call_doesnt_match} |
| 195 |
#| error: true |
| 196 |
expect_call(m, 1, summary(x)) |
| 197 |
#> Error: expected call summary(x) does not mach actual call summary(iris). |
| 198 |
``` |
| 199 |
|
| 200 |
### Verify values of argument |
| 201 |
|
| 202 |
Finally, one can verify whether the actual values of arguments passed |
| 203 |
to the mock function match the expectation. Following the previous |
| 204 |
example of `summary(iris)` we can make sure that the `object` parameter |
| 205 |
passed to `m()` was actually the `iris` dataset. |
| 206 |
|
| 207 |
```{r expect_args} |
| 208 |
expect_args(m, 1, iris) |
| 209 |
``` |
| 210 |
|
| 211 |
Here is what happens if the value turns out to be different. |
| 212 |
|
| 213 |
```{r expect_args_different} |
| 214 |
#| error: true |
| 215 |
expect_args(m, 1, iris[-1, ]) |
| 216 |
``` |
| 217 |
|
| 218 |
If the call has been made with an explicit argument name the same has |
| 219 |
to appear in `expect_args()`. |
| 220 |
|
| 221 |
```{r expect_args_named} |
| 222 |
#| error: true |
| 223 |
m <- mock(1) |
| 224 |
with_mocked_bindings(summary = m, { |
| 225 |
summary(object = iris) |
| 226 |
}) |
| 227 |
|
| 228 |
expect_args(m, 1, object = iris) |
| 229 |
``` |
| 230 |
|
| 231 |
Omitting the name results in an error. |
| 232 |
|
| 233 |
```{r expect_args_unnamed} |
| 234 |
#| error: true |
| 235 |
expect_args(m, 1, iris) |
| 236 |
``` |
| 237 |
|
| 238 |
|
| 239 |
## Further reading |
| 240 |
|
| 241 |
More information can be found in examples presented in manual pages |
| 242 |
for `?mock` and `?expect_call`. Extensive information about testing |
| 243 |
in R can be found in the documentation for the |
| 244 |
[testthat](https://github.com/r-lib/testthat) package. |