| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/stub.R |
| 3 |
\name{stub} |
| 4 |
\alias{stub} |
| 5 |
\title{Replace a function with a stub.} |
| 6 |
\usage{ |
| 7 |
stub(where, what, how, depth = 1) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{where}{Function to be called that will in turn call |
| 11 |
\code{what}.} |
| 12 |
|
| 13 |
\item{what}{Name of the function you want to stub out (a |
| 14 |
\code{character} string).} |
| 15 |
|
| 16 |
\item{how}{Replacement function (also a \code{\link{mock}} function) |
| 17 |
or a return value for which a function will be created |
| 18 |
automatically.} |
| 19 |
|
| 20 |
\item{depth}{Specifies the depth to which the function should be stubbed} |
| 21 |
} |
| 22 |
\description{ |
| 23 |
The result of calling \code{stub} is that, when \code{where} |
| 24 |
is invoked and when it internally makes a call to \code{what}, |
| 25 |
\code{how} is going to be called instead. |
| 26 |
} |
| 27 |
\details{ |
| 28 |
This is much more limited in scope in comparison to |
| 29 |
`testthat::with_mock()` which effectively replaces |
| 30 |
\code{what} everywhere. In other words, when using \code{with_mock} |
| 31 |
and regardless of the number of intermediate calls, \code{how} is |
| 32 |
always called instead of \code{what}. However, using this API, |
| 33 |
the replacement takes place only for a single function \code{where} |
| 34 |
and only for calls originating in that function. |
| 35 |
} |
| 36 |
\examples{ |
| 37 |
f <- function() TRUE |
| 38 |
g <- function() f() |
| 39 |
stub(g, 'f', FALSE) |
| 40 |
|
| 41 |
# now g() returns FALSE because f() has been stubbed out |
| 42 |
g() |
| 43 |
|
| 44 |
# you can stub multiple functions by calling stub() multiple times |
| 45 |
f <- function() TRUE |
| 46 |
g <- function() TRUE |
| 47 |
h <- function() any(f(), g()) |
| 48 |
stub(h, 'f', FALSE) |
| 49 |
stub(h, 'g', FALSE) |
| 50 |
|
| 51 |
# now h() returns FALSE because both f() and g() have been stubbed out |
| 52 |
h() |
| 53 |
|
| 54 |
} |