A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/krlmlr/mockr below:

krlmlr/mockr: Drop-in replacement for testthat::with_mock()

The goal of mockr is to provide a drop-in replacement for testthat::local_mock() and testthat::with_mock() which is deprecated in testthat 3.0.0. The functions mockr::local_mock() and mockr::with_mock() are modeled closely after the original implementation, but now only allow 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.
  2. You cannot refer to functions in your package via your.package:: or your.package::: anymore.

If you encounter other problems, please file an issue.

library(mockr)

access_resource <- function() {
  message("Trying to access resource...")
  # For some reason we can't access the resource in our tests.
  stop("Can't access resource now.")
}

work_with_resource <- function() {
  resource <- access_resource()
  message("Fetched resource: ", resource)
  invisible(resource)
}

# Calling this function gives an error
work_with_resource()
#> Trying to access resource...
#> Error in access_resource(): Can't access resource now.

local({
  # Here, we override the function that raises the error
  local_mock(access_resource = function() 42)

  # No error raised
  work_with_resource()
})
#> Fetched resource: 42

Install from CRAN via

install.packages("mockr")

Please note that the mockr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4