testthat
Mock object, which is a part of
the mockery
package, started as an extension to testthat’s
with_mock()
facility. Its main purpose was to simplify the
replacement (mocking) of a given function by means of
with_mock
and the later verification of actual calls
invoked on the replacing function.
The mockery
package which provides its own stubbing
facility, the stub()
function. Here, however, we will look
only at how mock()
can be used together with
with_mock()
.
mock
functionMocking is a well-known technique when it comes to unit-testing and
in most languages there is some notion of a mock object. In R, however,
the natural equivalent of a mock object is a mock function -
and this is exactly what a call to mock()
will produce.
Let’s look at arguments accepted by the mock()
factory function. The main is a list of values which will be
returned upon subsequent calls to m
.
mock()
can take also an expression which will be
evaluated upon a call.
By default, if the total number of calls exceeds the number of
defined return values, the mock function will throw an
exception. However, one can also choose to cycle through the list of
retun values by setting the cycle
argument of
mock()
to TRUE
.
m <- mock(1, 2)
m()
#> [1] 1
m()
#> [1] 2
m()
#> Error: too many calls to mock object and cycle set to FALSE
If a return value is defined by an expression, this expression will be evaluated each time a cycle reaches its position.
with_mock()
Using mock functions with testthat
’s
with_mock()
is pretty straightforward.
The mockery
package comes with a few additional
expectations which might turn out to be a very useful extension to
testthat
’s API. One can for example verify the number and
signature of calls invoked on a mock function, as well as the values of
arguments passed in those calls.
First, let’s make sure the mocked function is called exactly as many
times as we expect. This can be done with
expect_called()
.
And here is what happens when we get the number of calls wrong.
Another new expectation is expect_call()
which compares
the signature of the actual call as invoked on the mock function with
the expected one. It takes as arguments: the mock function, the call
number, expected call.
And here is what happens if the call doesn’t match.
Finally, one can verify whether the actual values of arguments passed
to the mock function match the expectation. Following the previous
example of summary(iris)
we can make sure that the
object
parameter passed to m()
was actually
the iris
dataset.
Here is what happens if the value turns out to be different.
expect_args(m, 1, iris[-1, ])
#> Error: arguments to call #1 not equal to expected arguments.
#> Component 1: Attributes: < Component "row.names": Numeric: lengths (150, 149) differ >
#> Component 1: Component 1: Numeric: lengths (150, 149) differ
#> Component 1: Component 2: Numeric: lengths (150, 149) differ
#> Component 1: Component 3: Numeric: lengths (150, 149) differ
#> Component 1: Component 4: Numeric: lengths (150, 149) differ
#> Component 1: Component 5: Lengths: 150, 149
#> Component 1: Component 5: Lengths (150, 149) differ (string compare on first 149)
#> Component 1: Component 5: 2 string mismatches
#> expected argument list does not mach actual one.
If the call has been made with an explicit argument name the same has
to appear in expect_args()
.
m <- mock(1)
with_mock(summary = m, {
summary(object = iris)
})
#> [1] 1
expect_args(m, 1, object = iris)
Omitting the name results in an error.
More information can be found in examples presented in manual pages
for ?mock
and ?expect_call
. Extensive
information about testing in R can be found in the documentation for the
testthat package.