Description Usage Arguments Examples
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.
1 | stub(fn, key, character.only = FALSE) <- value
|
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 |
value |
ANY. The new value of the stubbed key. |
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.