mockr v0.1


Monthly downloads



Mocking in R

Provides a means to mock a package function, i.e., temporarily substitute it for testing. Designed as a drop-in replacement for 'testthat::with_mock()', which may break in R 3.4.0 and later.


mockr Travis-CI Build Status Coverage Status CRAN\_Status\_Badge

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. There are some caveats, though:

  1. Mocking external functions (in other packages) doesn't work anymore. This is by design.

    • If you need to mock an external function, write a wrapper.
    • If that external function is called by a third-party function, you'll need to perhaps mock that third-party function, or look for a different way of implementing this test or organizing your code.
  2. You cannot refer to functions in your package via your.package:: or your.package::: anymore, this is a limitation of the implementation.

    • Simply remove the your.package:::, your code and tests should run just fine without that.

If you encounter other problems, please file an issue.


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
#> [1] 42

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


Install from GitHub via


Functions in mockr

Name Description
mockr-package mockr: Mocking in R
with_mock Mock functions in a package.
No Results!

Last month downloads


License GPL-3
Encoding UTF-8
LazyData true
Date 2017-04-28
RoxygenNote 6.0.1
NeedsCompilation no
Packaged 2017-04-29 08:10:46 UTC; muelleki
Repository CRAN
Date/Publication 2017-04-29 13:18:38 UTC
suggests covr , testthat
imports lazyeval

Include our badge in your README