File: with_mock.Rd

package info (click to toggle)
r-cran-testthat 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,996 kB
  • sloc: cpp: 9,265; ansic: 37; sh: 15; makefile: 5
file content (82 lines) | stat: -rw-r--r-- 2,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mock.R
\name{with_mock}
\alias{with_mock}
\alias{local_mock}
\title{Mock functions in a package.}
\usage{
with_mock(..., .env = topenv())

local_mock(..., .env = topenv(), .local_envir = parent.frame())
}
\arguments{
\item{...}{named parameters redefine mocked functions, unnamed parameters
will be evaluated after mocking the functions}

\item{.env}{the environment in which to patch the functions,
defaults to the top-level environment.  A character is interpreted as
package name.}

\item{.local_env}{Environment in which to add exit hander.
For expert use only.}
}
\value{
The result of the last unnamed parameter
}
\description{
\ifelse{html}{\out{<a href='https://www.tidyverse.org/lifecycle/#superseded'><img src='figures/lifecycle-superseded.svg' alt='Superseded lifecycle'></a>}}{\strong{Superseded}}

\code{with_mock()} and \code{local_mock()} are superseded in favour of the more
rigorous techniques found in the \href{https://krlmlr.github.io/mockr/}{mockr}
and \href{https://github.com/r-lib/mockery#mockery}{mockery} packages.

Mocking allows you to temporary replace the implementation of functions
within a package, which useful for testing code that relies on functions
that are slow, have unintended side effects or access resources that may
not be available when testing.

This works by using some C code to temporarily modify the mocked function
\emph{in place}. On exit, all functions are restored to their previous state.
This is somewhat abusive of R's internals so use with care. In particular,
functions in base packages cannot be mocked; to work aroud you'll need to
make a wrapper function in your own package..
}
\section{3rd edition}{

\ifelse{html}{\figure{lifecycle-deprecated.svg}{options: alt='Deprecated lifecycle'}}{\strong{Deprecated}}

\code{with_mock()} and \code{local_mock()} are deprecated in the third edition.
}

\examples{
add_one <- function(x) x + 1
expect_equal(add_one(2), 3)
with_mock(
  add_one = function(x) x - 1,
  expect_equal(add_one(2), 1)
)
square_add_one <- function(x) add_one(x)^2
expect_equal(square_add_one(2), 9)
expect_equal(
  with_mock(
    add_one = function(x) x - 1,
    square_add_one(2)
  ),
  1
)

# local_mock() -------------------------------
plus <- function(x, y) x + y
test_that("plus(1, 1) == 2", {
  expect_equal(plus(1, 1), 2)
})

test_that("plus(1, 1) == 3", {
  local_mock(plus = function(x, y) 3)
  expect_equal(plus(1, 1), 3)
})
}
\references{
Suraj Gupta (2012): \href{http://blog.obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/}{How R Searches And Finds Stuff}
}
\keyword{internal}