with_mock {mockr} | R Documentation |
Executes code after temporarily substituting implementations of package functions. This is useful for testing code that relies on functions that are slow, have unintended side effects or access resources that may not be available when testing.
with_mock(..., .parent = parent.frame(), .env = topenv(.parent))
... |
|
.parent |
|
.env |
|
This works by adding a shadow environment as a parent of the environment
in which the expressions are evaluated. Everything happens at the R level,
but only functions in your own package can be mocked.
Otherwise, the implementation is modeled after the original version in the
testthat
pacakge, which is now deprecated.
The result of the last unnamed parameter, visibility is preserved
Suraj Gupta (2012): How R Searches And Finds Stuff
some_func <- function() stop("oops") some_other_func <- function() some_func() tester_func <- function() { with_mock( some_func = function() 42, some_other_func() ) } try(some_other_func()) tester_func()