Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krlmlr/mockr
Drop-in replacement for testthat::with_mock()
https://github.com/krlmlr/mockr
Last synced: 6 days ago
JSON representation
Drop-in replacement for testthat::with_mock()
- Host: GitHub
- URL: https://github.com/krlmlr/mockr
- Owner: krlmlr
- Created: 2016-12-23T22:40:26.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T00:51:16.000Z (24 days ago)
- Last Synced: 2024-11-07T06:11:49.315Z (13 days ago)
- Language: R
- Homepage: https://krlmlr.github.io/mockr/
- Size: 446 KB
- Stars: 18
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- jimsghstars - krlmlr/mockr - Drop-in replacement for testthat::with_mock() (R)
README
---
output: downlit::readme_document
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)pkgload::load_all()
```# mockr
[![rcc](https://github.com/krlmlr/mockr/workflows/rcc/badge.svg)](https://github.com/krlmlr/mockr/actions)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/mockr)](https://cran.r-project.org/package=mockr)
[![Codecov test coverage](https://codecov.io/gh/krlmlr/mockr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/krlmlr/mockr?branch=main)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.
- If you need to mock an external function, write a wrapper.
- If that external function is called by third-party code, you'll need to perhaps mock that third-party code, 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.
- Remove the `your.package:::`, your code and tests should run just fine without that.If you encounter other problems, please [file an issue](https://github.com/krlmlr/mockr/issues).
## Example
```{r example, error = TRUE}
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()local({
# Here, we override the function that raises the error
local_mock(access_resource = function() 42)# No error raised
work_with_resource()
})
```## Installation
Install from CRAN via
```r
install.packages("mockr")
```---
## Code of Conduct
Please note that the mockr project is released with a [Contributor Code of Conduct](https://krlmlr.github.io/mockr/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.