stub: Replace a function with a stub.

stubR Documentation

Replace a function with a stub.

Description

The result of calling stub is that, when where is invoked and when it internally makes a call to what, how is going to be called instead.

Usage

stub(where, what, how, depth = 1)

Arguments

where

Function to be called that will in turn call what.

what

Name of the function you want to stub out (a character string).

how

Replacement function (also a mock function) or a return value for which a function will be created automatically.

depth

Specifies the depth to which the function should be stubbed

Details

This is much more limited in scope in comparison to with_mock which effectively replaces what everywhere. In other words, when using with_mock and regardless of the number of intermediate calls, how is always called instead of what. However, using this API, the replacement takes place only for a single function where and only for calls originating in that function.

Examples

f <- function() TRUE
g <- function() f()
stub(g, 'f', FALSE)

# now g() returns FALSE because f() has been stubbed out
g()

# you can stub multiple functions by calling stub() multiple times 
f <- function() TRUE
g <- function() TRUE
h <- function() any(f(), g())
stub(h, 'f', FALSE)
stub(h, 'g', FALSE)

# now h() returns FALSE because both f() and g() have been stubbed out
h()


mockery documentation built on Sept. 27, 2023, 1:07 a.m.