Mocking Library for R

The two main functionalities of this package are creating mock objects (functions) and selectively intercepting calls to a given function that originate in some other function. It can be used with any testing framework available for R. Mock objects can be injected with either this package's own stub() function or a similar with_mock() facility present in the testthat package.

A mocking library for R.

To install the latest CRAN release:

To install directly from the source code in this github repository:

> # If you don't have devtools installed yet:
> install.packages('devtools')
> # Then:
> library('devtools')
> devtools::install_github('n-s-f/mockery')

Mockery provides the capacity for stubbing out functions and for verifying function calls during testing.

Mockery's stub function will let you stub out a function with another function or simply a return value. Note that if you choose to replace the function with another function, the signatures of the two functions should be compatible.

g = function(y) y
f = function(x) g(x) + 1
test_that('demonstrate stubbing', {
    # replaces 'g' with a function that always returns 100
    # but only when called from f
    stub(f, 'g', 100)
    # this can also be written
    stub(f, 'g', function(...) 100)
    expect_equal(f(1), 101)

Stubbing works with classes of all descriptions and namespaced functions:

# this stubs the 'request_perform' function, but only
# for httr::get, and only when it is called from within this
# test function
stub(httr::GET, 'request_perform', 'some html')
# it is also possible to stub out a namespaced function call
stub(some_function, 'namespace::function', 'some return value')

This also works with R6 classes and methods.

For more examples, please see the test code contained in this repository.

Mock objects allow you to specify the behavior of the function you've stubbed out while also verifying how that function was used.

g = function(y) y
f = function(x) g(x) + 1
test_that('demonstrate mock object usage', {
    # mocks can specify behavior
    mock = mock(100)
    stub(f, 'g', mock)
    result = g(5)
    expect_equal(result, 101)
    # and allow you to make assertions on the mock was treated
    expect_called(mock, 1)
    expect_args(mock, 5)

You can also specify multiple return values

mock = mock(1, "a", sqrt(3))

and access the arguments with which it was called.

mock <- mock()
mock(x = 1)
mock(y = 2)
expect_equal(length(mock), 2)
args <- mock_args(mock)
expect_equal(args[[1]], list(x = 1))
expect_equal(args[[2]], list(y = 2))

Please report bugs and feature requests through github issues.


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.3.0 by Noam Finkelstein, 2 months ago

Report a bug at

Browse source code at

Authors: Noam Finkelstein [aut, cre], Lukasz Bartnik [aut]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports testthat, utils

Suggests knitr, rmarkdown

Suggested by prettycode, subprocess, tracer.

See at CRAN