| 1 |
testthat::context('stub') |
| 2 |
|
| 3 |
a = 10 |
| 4 |
f = function(x) x |
| 5 |
g = function(x) f(x) + a |
| 6 |
test_that('stubs function with return value', { |
| 7 |
# before stubbing |
| 8 |
expect_equal(g(20), 30) |
| 9 |
|
| 10 |
# when |
| 11 |
stub(g, 'f', 100) |
| 12 |
|
| 13 |
# then |
| 14 |
expect_equal(g(20), 110) |
| 15 |
}) |
| 16 |
|
| 17 |
test_that('values restored after test', { |
| 18 |
expect_equal(f(15), 15) |
| 19 |
expect_equal(g(15), 25) |
| 20 |
}) |
| 21 |
|
| 22 |
test_that('stubs function with function', { |
| 23 |
# given |
| 24 |
a = 10 |
| 25 |
f = function(x) x |
| 26 |
g = function(x) f(x) + a |
| 27 |
|
| 28 |
# before stubbing |
| 29 |
expect_equal(g(20), 30) |
| 30 |
|
| 31 |
# when |
| 32 |
stub(g, 'f', function(...) 500) |
| 33 |
|
| 34 |
# then |
| 35 |
expect_equal(g(10), 510) |
| 36 |
}) |
| 37 |
|
| 38 |
|
| 39 |
test_that('stubs function from namespace', { |
| 40 |
# given |
| 41 |
f = function() testthat::capture_output(print('hello')) |
| 42 |
|
| 43 |
# before stubbing |
| 44 |
expect_true(grepl('hello', f())) |
| 45 |
|
| 46 |
# when |
| 47 |
stub(f, 'testthat::capture_output', 10) |
| 48 |
|
| 49 |
# then |
| 50 |
expect_equal(f(), 10) |
| 51 |
}) |
| 52 |
|
| 53 |
test_that('does not stub other namespeaced functions', { |
| 54 |
# given |
| 55 |
f = function() { |
| 56 |
a = testthat::capture_output(print('hello')) |
| 57 |
b = testthat::is_null('not null') |
| 58 |
return(c(a, b)) |
| 59 |
} |
| 60 |
|
| 61 |
# when |
| 62 |
stub(f, 'testthat::is_null', 'stubbed output') |
| 63 |
|
| 64 |
# then |
| 65 |
result = f() |
| 66 |
expect_true(grepl('hello', result[1])) |
| 67 |
expect_equal(result[2], 'stubbed output') |
| 68 |
}) |
| 69 |
|
| 70 |
test_that('stub multiple functions', { |
| 71 |
# given |
| 72 |
f = function(x) x + 10 |
| 73 |
g = function(y) y + 20 |
| 74 |
h = function(z) f(z) + g(z) |
| 75 |
|
| 76 |
# when |
| 77 |
stub(h, 'f', 300) |
| 78 |
stub(h, 'g', 500) |
| 79 |
|
| 80 |
# then |
| 81 |
expect_equal(h(1), 800) |
| 82 |
}) |
| 83 |
|
| 84 |
test_that('stub multiple namespaced functions', { |
| 85 |
# given |
| 86 |
h = function(x) mockery::stub(x) + mockery::get_function_source(x) |
| 87 |
|
| 88 |
# when |
| 89 |
stub(h, 'mockery::stub', 300) |
| 90 |
stub(h, 'mockery::get_function_source', 500) |
| 91 |
|
| 92 |
# then |
| 93 |
expect_equal(h(1), 800) |
| 94 |
}) |
| 95 |
|
| 96 |
test_that('stub works with do.call', { |
| 97 |
# given |
| 98 |
f = function(x) x + 10 |
| 99 |
g = function(x) do.call('f', list(x)) |
| 100 |
|
| 101 |
# before stub |
| 102 |
expect_equal(g(10), 20) |
| 103 |
|
| 104 |
# stub |
| 105 |
stub(g, 'f', 100) |
| 106 |
|
| 107 |
# then |
| 108 |
expect_equal(g(10), 100) |
| 109 |
}) |
| 110 |
|
| 111 |
test_that('stub works with lapply', { |
| 112 |
# given |
| 113 |
f = function(x) x + 10 |
| 114 |
g = function(x) lapply(x, 'f') |
| 115 |
l = list(1, 2, 3) |
| 116 |
|
| 117 |
# before stub |
| 118 |
expect_equal(g(l), list(11, 12, 13)) |
| 119 |
|
| 120 |
# stub |
| 121 |
stub(g, 'f', 100) |
| 122 |
|
| 123 |
# then |
| 124 |
expect_equal(g(l), list(100, 100, 100)) |
| 125 |
}) |
| 126 |
|
| 127 |
test_that('stub works well with mock object', { |
| 128 |
# given |
| 129 |
f = function(x) x + 10 |
| 130 |
g = function(x) f(x) |
| 131 |
|
| 132 |
mock_object = mock(100) |
| 133 |
stub(g, 'f', mock_object) |
| 134 |
|
| 135 |
# when |
| 136 |
result = g(5) |
| 137 |
|
| 138 |
# then |
| 139 |
expect_equal(result, 100) |
| 140 |
}) |
| 141 |
|
| 142 |
f = function(x) x + 10 |
| 143 |
g = function(x) f(x) |
| 144 |
test_that('mock object returns value', { |
| 145 |
mock_object = mock(1) |
| 146 |
stub(g, 'f', mock_object) |
| 147 |
|
| 148 |
expect_equal(g('anything'), 1) |
| 149 |
expect_called(mock_object, 1) |
| 150 |
expect_args(mock_object, 1, 'anything') |
| 151 |
}) |
| 152 |
|
| 153 |
test_that('mock object multiple return values', { |
| 154 |
mock_object = mock(1, "a", sqrt(3)) |
| 155 |
stub(g, 'f', mock_object) |
| 156 |
|
| 157 |
expect_equal(g('anything'), 1) |
| 158 |
expect_equal(g('anything'), "a") |
| 159 |
expect_equal(g('anything'), sqrt(3)) |
| 160 |
}) |
| 161 |
|
| 162 |
test_that('mock object accessing values of arguments', { |
| 163 |
mock_object <- mock() |
| 164 |
mock_object(x = 1) |
| 165 |
mock_object(y = 2) |
| 166 |
|
| 167 |
expect_equal(length(mock_object), 2) |
| 168 |
args <- mock_args(mock_object) |
| 169 |
|
| 170 |
expect_equal(args[[1]], list(x = 1)) |
| 171 |
expect_equal(args[[2]], list(y = 2)) |
| 172 |
}) |
| 173 |
|
| 174 |
test_that('mock object accessing call expressions', { |
| 175 |
mock_object <- mock() |
| 176 |
mock_object(x = 1) |
| 177 |
mock_object(y = 2) |
| 178 |
|
| 179 |
expect_equal(length(mock_object), 2) |
| 180 |
calls <- mock_calls(mock_object) |
| 181 |
|
| 182 |
expect_equal(calls[[1]], quote(mock_object(x = 1))) |
| 183 |
expect_equal(calls[[2]], quote(mock_object(y = 2))) |
| 184 |
}) |
| 185 |
|
| 186 |
library(R6) |
| 187 |
|
| 188 |
some_other_class = R6Class("some_class", |
| 189 |
public = list( |
| 190 |
external_method = function() {return('this is external output')} |
| 191 |
) |
| 192 |
) |
| 193 |
|
| 194 |
some_class = R6Class("some_class", |
| 195 |
public = list( |
| 196 |
some_method = function() {return(some_function())}, |
| 197 |
some_method_prime = function() {return(some_function())}, |
| 198 |
other_method = function() {return('method in class')}, |
| 199 |
method_without_other = function() { self$other_method() }, |
| 200 |
method_with_other = function() { |
| 201 |
other <- some_other_class$new() |
| 202 |
other$external_method() |
| 203 |
self$other_method() |
| 204 |
} |
| 205 |
) |
| 206 |
) |
| 207 |
|
| 208 |
# Calling function from R6 method |
| 209 |
some_function = function() {return("called from within class")} |
| 210 |
obj = some_class$new() |
| 211 |
test_that('stub works with R6 methods', { |
| 212 |
stub(obj$some_method, 'some_function', 'stub has been called') |
| 213 |
expect_equal(obj$some_method(), 'stub has been called') |
| 214 |
}) |
| 215 |
|
| 216 |
test_that('stub works with R6 methods that call internal methods in them', { |
| 217 |
stub(obj$method_without_other, 'self$other_method', 'stub has been called') |
| 218 |
expect_equal(obj$method_without_other(), 'stub has been called') |
| 219 |
}) |
| 220 |
|
| 221 |
test_that('stub works with R6 methods that have other objects in them', { |
| 222 |
stub(obj$method_with_other, 'self$other_method', 'stub has been called') |
| 223 |
expect_equal(obj$method_with_other(), 'stub has been called') |
| 224 |
}) |
| 225 |
|
| 226 |
test_that('R6 method does not stay stubbed', { |
| 227 |
expect_equal(obj$some_method(), 'called from within class') |
| 228 |
}) |
| 229 |
|
| 230 |
# Calling R6 method from function |
| 231 |
other_func = function() { |
| 232 |
obj = some_class$new() |
| 233 |
return(obj$other_method()) |
| 234 |
} |
| 235 |
test_that('stub works for stubbing R6 methods from within function calls', { |
| 236 |
stub(other_func, 'obj$other_method', 'stubbed R6 method') |
| 237 |
expect_equal(other_func(), 'stubbed R6 method') |
| 238 |
}) |
| 239 |
|
| 240 |
test_that('stub does not stay in effect', { |
| 241 |
expect_equal(other_func(), 'method in class') |
| 242 |
}) |
| 243 |
|
| 244 |
test_that('stub out of namespaced functions', { |
| 245 |
expect_true(grepl('hello', testthat::capture_output(print('hello')))) |
| 246 |
stub(testthat::capture_output, 'paste0', 'stubbed function') |
| 247 |
expect_equal(testthat::capture_output(print('hello')), 'stubbed function') |
| 248 |
}) |
| 249 |
|
| 250 |
test_that('stub multiple namespaced and R6 functions from within test env', { |
| 251 |
stub(testthat::capture_output, 'paste0', 'stub 1') |
| 252 |
stub(obj$some_method, 'some_function', 'stub 2') |
| 253 |
stub(obj$some_method_prime, 'some_function', 'stub 3') |
| 254 |
stub(testthat::test_that, 'test_code', 'stub 4') |
| 255 |
|
| 256 |
# all stubs are active |
| 257 |
expect_equal(testthat::capture_output(print('hello')), 'stub 1') |
| 258 |
expect_equal(obj$some_method(), 'stub 2') |
| 259 |
expect_equal(obj$some_method_prime(), 'stub 3') |
| 260 |
expect_equal(testthat::test_that('a', print), 'stub 4') |
| 261 |
|
| 262 |
# non mocked R6 and namespaced functions work as expected |
| 263 |
expect_equal(obj$other_method(), 'method in class') |
| 264 |
testthat::expect_failure(expect_equal(4, 5)) |
| 265 |
}) |
| 266 |
|
| 267 |
h = function(x) 'called h' |
| 268 |
g = function(x) h(x) |
| 269 |
f = function(x) g(x) |
| 270 |
test_that('use can specify depth of mocking', { |
| 271 |
stub_string = 'called stub!' |
| 272 |
stub(f, 'h', stub_string, depth=2) |
| 273 |
expect_equal(f(1), stub_string) |
| 274 |
}) |
| 275 |
|
| 276 |
h = function(x) 'called h' |
| 277 |
g = function(x) h(x) |
| 278 |
f = function(x) paste0(h(x), g(x)) |
| 279 |
test_that('mocked function is mocked at all depths', { |
| 280 |
stub_string = 'called stub!' |
| 281 |
stub(f, 'h', stub_string, depth=2) |
| 282 |
expect_equal(f(1), 'called stub!called stub!') |
| 283 |
}) |
| 284 |
|
| 285 |
h = function(x) 'called h' |
| 286 |
g = function(x) h(x) |
| 287 |
r = function(x) g(x) |
| 288 |
f = function(x) paste0(h(x), r(x)) |
| 289 |
test_that('mocked function is mocked at all depths', { |
| 290 |
stub_string = 'called stub!' |
| 291 |
stub(f, 'h', stub_string, depth=3) |
| 292 |
expect_equal(f(1), 'called stub!called stub!') |
| 293 |
}) |
| 294 |
|
| 295 |
h = function(x) 'called h' |
| 296 |
t = function(x) h(x) |
| 297 |
g = function(x) h(x) |
| 298 |
r = function(x) paste0(t(x), g(x)) |
| 299 |
u = function(x) paste0(h(x), g(x)) |
| 300 |
|
| 301 |
f = function(x) paste0(h(x), r(x), u(x)) |
| 302 |
|
| 303 |
t_env = new.env(parent=baseenv()) |
| 304 |
assign('h', h, t_env) |
| 305 |
environment(t) = t_env |
| 306 |
|
| 307 |
u_env = new.env(parent=baseenv()) |
| 308 |
assign('g', g, u_env) |
| 309 |
assign('h', h, u_env) |
| 310 |
environment(u) = u_env |
| 311 |
|
| 312 |
f_env = new.env(parent=baseenv()) |
| 313 |
assign('u', u, f_env) |
| 314 |
assign('h', h, f_env) |
| 315 |
assign('r', r, f_env) |
| 316 |
environment(f) = f_env |
| 317 |
|
| 318 |
a = function(x) x |
| 319 |
environment(a) = f_env |
| 320 |
|
| 321 |
test_that('mocked function is mocked at all depths across paths', { |
| 322 |
stub_string = 'called stub!' |
| 323 |
stub(f, 'h', stub_string, depth=4) |
| 324 |
expect_equal(f(1), 'called stub!called stub!called stub!called stub!called stub!') |
| 325 |
}) |
| 326 |
|
| 327 |
.a = function(x) h(x) |
| 328 |
test_that('mocks hidden functions', { |
| 329 |
stub_string = 'called stub!' |
| 330 |
stub(.a, 'h', stub_string, depth=4) |
| 331 |
expect_equal(f(1), 'called stub!called stub!called stub!called stub!called stub!') |
| 332 |
}) |
| 333 |
|
| 334 |
test_that("Does not error if function contains double quoted assignment functions", { |
| 335 |
f <- function(x, nms) { |
| 336 |
base::names(x) <- base::tolower(nms) |
| 337 |
x |
| 338 |
} |
| 339 |
|
| 340 |
stub(f, "base::tolower", toupper) |
| 341 |
expect_equal(f(1, "b"), c(B = 1)) |
| 342 |
|
| 343 |
stub(f, "base::names<-", function(x, value) stats::setNames(x, "d")) |
| 344 |
expect_equal(f(1, "b"), c(d = 1)) |
| 345 |
}) |
| 346 |
|
| 347 |
h = function(x) x |
| 348 |
g = function(x) h(x) |
| 349 |
f = function(x) g(x) |
| 350 |
|
| 351 |
locked_f_env = new.env(parent = baseenv()) |
| 352 |
assign('g', g, locked_f_env) |
| 353 |
assign('h', h, locked_f_env) |
| 354 |
environment(f) <- locked_f_env |
| 355 |
lockEnvironment(locked_f_env, bindings=TRUE) |
| 356 |
|
| 357 |
test_that('stubs locked functions', { |
| 358 |
stub_string = 'called stub!' |
| 359 |
stub(f, 'h', stub_string, depth=2) |
| 360 |
expect_equal(f('not stub'), 'called stub!') |
| 361 |
}) |