| 1 |
|
| 2 |
#' Create and query a mocked function. |
| 3 |
#' |
| 4 |
#' Mock object's primary use is to record calls that are made on the |
| 5 |
#' mocked function. |
| 6 |
#' |
| 7 |
#' Optionally values/expressions can be passed via \code{...} for the |
| 8 |
#' mock object to return them upon subsequent calls. Expressions are |
| 9 |
#' evaluated in environment \code{envir} before being returned. If no |
| 10 |
#' value is passed in \code{...} then \code{NULL} is returned. |
| 11 |
#' |
| 12 |
#' Passing an expression or a function call via \code{...} is also a |
| 13 |
#' way to implement side effects: keep track of the state of code |
| 14 |
#' under testing, throw an exception when a condition is met, etc. |
| 15 |
#' |
| 16 |
#' \code{mock_calls} and \code{mock_args} can be used to access the |
| 17 |
#' list of calls made on a mocked function and a respective list of |
| 18 |
#' values of arguments passed to each of these calls. |
| 19 |
#' |
| 20 |
#' |
| 21 |
#' @param ... Values returned upon subsequent calls. |
| 22 |
#' @param cycle Whether to cycle over the return values. If \code{FALSE}, |
| 23 |
#' will fail if called too many times. |
| 24 |
#' @param envir Where to evaluate the expressions being returned. |
| 25 |
#' @param m A \code{\link{mock}}ed function. |
| 26 |
#' @param x A \code{\link{mock}}ed function. |
| 27 |
#' |
| 28 |
#' @return \code{mock()} returns a mocked function which can be then used elsewhere}. |
| 29 |
#' @return \code{mock_args()} returns a \code{list} of \code{list}s |
| 30 |
#' of argument values. |
| 31 |
#' @return \code{mock_calls()} returns a \code{list} of \code{call}s. |
| 32 |
#' @return \code{length.mock()} returns the number of calls invoked on \code{m}. |
| 33 |
#' |
| 34 |
#' @examples |
| 35 |
#' library(testthat) |
| 36 |
#' |
| 37 |
#' f <- function(x) summary(x) |
| 38 |
#' local({ |
| 39 |
#' m <- mock(1) |
| 40 |
#' stub(f, 'summary', m) |
| 41 |
#' |
| 42 |
#' expect_equal(f(iris), 1) |
| 43 |
#' expect_called(m, 1) |
| 44 |
#' expect_call(m, 1, summary(x)) |
| 45 |
#' expect_args(m, 1, iris) |
| 46 |
#' }) |
| 47 |
#' |
| 48 |
#' # multiple return values |
| 49 |
#' local({ |
| 50 |
#' m <- mock(1, "a", sqrt(3)) |
| 51 |
#' stub(f, 'summary', m) |
| 52 |
#' |
| 53 |
#' expect_equal(f(iris), 1) |
| 54 |
#' expect_equal(f(iris), "a") |
| 55 |
#' expect_equal(f(iris), 1.73, tolerance = .01) |
| 56 |
#' }) |
| 57 |
#' |
| 58 |
#' # side effects |
| 59 |
#' local({ |
| 60 |
#' m <- mock(1, 2, stop("error")) |
| 61 |
#' stub(f, 'summary', m) |
| 62 |
#' |
| 63 |
#' expect_equal(f(iris), 1) |
| 64 |
#' expect_equal(f(iris), 2) |
| 65 |
#' expect_error(f(iris), "error") |
| 66 |
#' }) |
| 67 |
#' |
| 68 |
#' # accessing call expressions |
| 69 |
#' m <- mock() |
| 70 |
#' m(x = 1) |
| 71 |
#' m(y = 2) |
| 72 |
#' expect_equal(length(m), 2) |
| 73 |
#' calls <- mock_calls(m) |
| 74 |
#' expect_equal(calls[[1]], quote(m(x = 1))) |
| 75 |
#' expect_equal(calls[[2]], quote(m(y = 2))) |
| 76 |
#' |
| 77 |
#' # accessing values of arguments |
| 78 |
#' m <- mock() |
| 79 |
#' m(x = 1) |
| 80 |
#' m(y = 2) |
| 81 |
#' expect_equal(length(m), 2) |
| 82 |
#' args <- mock_args(m) |
| 83 |
#' expect_equal(args[[1]], list(x = 1)) |
| 84 |
#' expect_equal(args[[2]], list(y = 2)) |
| 85 |
#' |
| 86 |
#' @name mock |
| 87 |
NULL |
| 88 |
|
| 89 |
|
| 90 |
#' @export |
| 91 |
#' @rdname mock |
| 92 |
#' |
| 93 |
#' @importFrom testthat fail |
| 94 |
mock <- function (..., cycle = FALSE, envir = parent.frame()) { |
| 95 |
stopifnot(is.environment(envir)) |
| 96 |
|
| 97 |
return_values <- eval(substitute(alist(...))) |
| 98 |
return_values_env <- envir |
| 99 |
call_no <- 0 |
| 100 |
calls <- list() |
| 101 |
args <- list() |
| 102 |
|
| 103 |
mock_impl <- function(...) { |
| 104 |
call_no <<- call_no + 1 |
| 105 |
calls[[call_no]] <<- match.call() |
| 106 |
|
| 107 |
args[[call_no]] <<- list(...) |
| 108 |
|
| 109 |
if (length(return_values)) { |
| 110 |
if (call_no > length(return_values) && !cycle) |
| 111 |
fail("too many calls to mock object and cycle set to FALSE") |
| 112 |
|
| 113 |
value <- return_values[[(call_no - 1) %% length(return_values) + 1]] |
| 114 |
return(eval(value, envir = return_values_env)) |
| 115 |
} |
| 116 |
|
| 117 |
# TODO maybe it should the mock object itself? |
| 118 |
invisible(NULL) |
| 119 |
} |
| 120 |
|
| 121 |
class(mock_impl) <- 'mock' |
| 122 |
mock_impl |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
#' @export |
| 127 |
#' @rdname mock |
| 128 |
mock_args <- function (m) { |
| 129 |
stopifnot(is_mock(m)) |
| 130 |
environment(m)$args |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
#' @export |
| 135 |
#' @rdname mock |
| 136 |
mock_calls <- function (m) { |
| 137 |
stopifnot(is_mock(m)) |
| 138 |
environment(m)$calls |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
is_mock <- function (object) inherits(object, 'mock') |
| 143 |
|
| 144 |
|
| 145 |
#' @export |
| 146 |
#' @rdname mock |
| 147 |
length.mock <- function (x) |
| 148 |
{ |
| 149 |
length(environment(x)$calls) |
| 150 |
} |
| 151 |
|
| 152 |
summary <- NULL |
| 153 |
lm <- NULL |