stub-set: Stub the environment of a closure.

Description Usage Arguments Examples

Description

When testing functions, sometimes we would like to not use certain downstream methods that get called by that function. For example, if we are testing an HTTP library, we may not want to make actual HTTP calls. We can "stub" away the functions in our main function so they perform a less complex but functionally similar behavior.

Usage

1
stub(fn, key, character.only = FALSE) <- value

Arguments

fn

function. The function to stub.

key

character. The variable/key in the closure to stub away. One can also specify the name (without quoting it, using non-standard evaluation).

character.only

logical. As with the character.only parameter of base::library, this will not use non-standard evaluation for key if set to TRUE. The default is FALSE.

value

ANY. The new value of the stubbed key.

Examples

1
2
3
4
5
6
7
8
fn <- function() { cat(paste0('a', 'b')) }
stub(fn, 'paste0') <- paste
fn() # Will print "a b" instead of "ab"
stub(fn, paste0) <- paste # This also works.

stubbed_key <- 'paste0'
stub(fn, stubbed_key, character.only = TRUE) <- paste
# We need to use character.only = TRUE for the above meta-programming to work

robertzk/testthatsomemore documentation built on Aug. 15, 2017, 7:45 a.m.