The goal of mockr is to provide a drop-in replacement for testthat::with_mock() which will be deprecated in the next version of testthat. The only exported function, with_mock(), is modeled closely after the original implementation, but now only allows mocking functions in the package under test. In contrast to the original implementation, no fiddling with R’s internals is needed, and the implementation plays well with byte-compiled code.

  • If you need to mock an external function, write a wrapper.
  • The original implementation allowed modifying the behavior of other packages’ functions. this is discouraged (and not possible with this implementation).


#> Loading mockr
some_func <- function() stop("oops")
some_other_func <- function() some_func()

# Calling this function gives an error
#> Error in some_func(): oops

tester_func <- function() {
  # Here, we override the function that raises the error
    some_func = function() 42,

# No error raised
#> Error: some_func not found in environment R_GlobalEnv.

# Mocking doesn't override functions in the same environment by design
with_mock(some_func = function() 6 * 7, some_other_func())
#> Error: some_func not found in environment R_GlobalEnv.


Install from GitHub via