Mockr
Drop-in replacement for testthat::with_mock()
Install / Use
/learn @krlmlr/MockrREADME
mockr
<!-- badges: start --> <!-- badges: end -->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:
- 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.
- You cannot refer to functions in your package via
your.package::oryour.package:::anymore.- Remove the
your.package:::, your code and tests should run just fine without that.
- Remove the
If you encounter other problems, please file an issue.
Example
<pre class='chroma'> <span class='kr'><a href='https://rdrr.io/r/base/library.html'>library</a></span><span class='o'>(</span><span class='nv'><a href='https://krlmlr.github.io/mockr/'>mockr</a></span><span class='o'>)</span> <span class='nv'>access_resource</span> <span class='o'><-</span> <span class='kr'>function</span><span class='o'>(</span><span class='o'>)</span> <span class='o'>{</span> <span class='nf'><a href='https://rdrr.io/r/base/message.html'>message</a></span><span class='o'>(</span><span class='s'>"Trying to access resource..."</span><span class='o'>)</span> <span class='c'># For some reason we can't access the resource in our tests.</span> <span class='kr'><a href='https://rdrr.io/r/base/stop.html'>stop</a></span><span class='o'>(</span><span class='s'>"Can't access resource now."</span><span class='o'>)</span> <span class='o'>}</span> <span class='nv'>work_with_resource</span> <span class='o'><-</span> <span class='kr'>function</span><span class='o'>(</span><span class='o'>)</span> <span class='o'>{</span> <span class='nv'>resource</span> <span class='o'><-</span> <span class='nf'>access_resource</span><span class='o'>(</span><span class='o'>)</span> <span class='nf'><a href='https://rdrr.io/r/base/message.html'>message</a></span><span class='o'>(</span><span class='s'>"Fetched resource: "</span>, <span class='nv'>resource</span><span class='o'>)</span> <span class='nf'><a href='https://rdrr.io/r/base/invisible.html'>invisible</a></span><span class='o'>(</span><span class='nv'>resource</span><span class='o'>)</span> <span class='o'>}</span> <span class='c'># Calling this function gives an error</span> <span class='nf'>work_with_resource</span><span class='o'>(</span><span class='o'>)</span> <span class='c'>#> Trying to access resource...</span> <span class='c'>#> Error in access_resource(): Can't access resource now.</span> <span class='nf'><a href='https://rdrr.io/r/base/eval.html'>local</a></span><span class='o'>(</span><span class='o'>{</span> <span class='c'># Here, we override the function that raises the error</span> <span class='nf'><a href='https://krlmlr.github.io/mockr/reference/local_mock.html'>local_mock</a></span><span class='o'>(</span>access_resource <span class='o'>=</span> <span class='kr'>function</span><span class='o'>(</span><span class='o'>)</span> <span class='m'>42</span><span class='o'>)</span> <span class='c'># No error raised</span> <span class='nf'>work_with_resource</span><span class='o'>(</span><span class='o'>)</span> <span class='o'>}</span><span class='o'>)</span> <span class='c'>#> Fetched resource: 42</span></pre>Installation
Install from CRAN via
<pre class='chroma'> <span class='nf'><a href='https://rdrr.io/r/utils/install.packages.html'>install.packages</a></span><span class='o'>(</span><span class='s'>"mockr"</span><span class='o'>)</span></pre>Code of Conduct
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.
