Drop-in replacement for testthat::with_mock()
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R fix a typo Jun 4, 2017
man
tests
.Rbuildignore CRAN comments Apr 28, 2017
.gitignore infra Dec 20, 2016
.travis.yml use tic Dec 23, 2016
DESCRIPTION update description Apr 29, 2017
NAMESPACE DESCRIPTION Dec 23, 2016
NEWS.md
README.Rmd use CRAN badge Apr 28, 2017
README.md
_pkgdown.yml use flatly Dec 23, 2016
codecov.yml codecov Dec 23, 2016
cran-comments.md
mockr.Rproj infra Dec 20, 2016
tic.R add to known hosts Jun 13, 2017

README.md

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.

Example

some_func <- function() stop("oops")
some_other_func <- function() some_func()

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

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

# No error raised
tester_func()
#> [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

Installation

Install from GitHub via

devtools::install_github("krlmlr/mockr")