Skip to content

[Experimental]

These functions represent a second attempt at bringing mocking to testthat, incorporating what we've learned from the mockr, mockery, and mockthat package.

with_mocked_bindings() and local_mocked_bindings() work by temporarily changing variable bindings in the namespace of namespace .package. Generally, it's only safe to mock packages that you own. If you mock other packages, we recommend using skip_on_cran() to avoid CRAN failures if the implementation changes.

These functions do not currently affect registered S3 methods.

Usage

local_mocked_bindings(..., .package = NULL, .env = caller_env())

with_mocked_bindings(code, ..., .package = NULL)

Arguments

...

Name-value pairs providing functions to mock.

.package

The name of the package where mocked functions should be inserted. Generally, you should not need to supply this as it will be automatically detected when whole package tests are run or when there's one package under active development (i.e. loaded with pkgload::load_all()).

.env

Environment that defines effect scope. For expert use only.

code

Code to execute with specified bindings.