Debian Codegraph @

tests/testthat/test-mock-object.R

raw
1 context("Mock objects")
2
3 test_that("mock single brackets", {
4 m <- mock(1)
5 m()
6 expect_equal(mock_calls(m)[[1]], bquote(m()))
7 })
8
9
10 test_that("mock length", {
11 m <- mock(1, cycle = TRUE)
12 expect_equal(length(m), 0)
13
14 m()
15 expect_equal(length(m), 1)
16
17 m()
18 m()
19 expect_equal(length(m), 3)
20 })
21
22
23 test_that("mock cyclic returns", {
24 m <- mock(1, cycle = TRUE)
25 expect_equal(lapply(1:10, m), as.list(rep(1, 10)))
26
27 m <- mock(1, 2, cycle = TRUE)
28 expect_equal(lapply(1:10, m), as.list(rep(1:2, 5)))
29
30 m <- mock(1, 2, 3, cycle = TRUE)
31 expect_equal(lapply(1:12, m), as.list(rep(1:3, 4)))
32 })
33
34
35 test_that("call list", {
36 m <- mock()
37 e <- environment(m)
38
39 local_mocked_bindings(summary = m)
40 summary(iris)
41
42 expect_true(exists('calls', envir = e))
43 expect_length(e$calls, 1)
44 expect_equal(e$calls[[1]], bquote(summary(iris)))
45 })
46
47
48 test_that("expect calls", {
49 m <- mock()
50
51 local_mocked_bindings(summary = m)
52 summary(iris)
53
54 expect_call(m, 1, summary(iris))
55 })
56
57
58 # test_that("error for long call is formatted well", {
59 # m <- mock()
60 # mockery::stub(read.csv, "read.table", m)
61 # read.csv('file.csv')
62
63 # # test mock with mock, how crazy is that!
64 # # we cannot call expect_failure because it messes up calls to expect()
65 # # thus we intercept calls to expect() and later compare arguments
66 # test_mock <- mock(TRUE, TRUE)
67 # with_mock(expect = test_mock, {
68 # expect_call(m, 1, x)
69 # })
70
71 # expect_called(test_mock, 2)
72
73 # err <- paste0(
74 # 'expected call x does not mach actual call ',
75 # 'read.table(file = file, header = header, sep = sep, quote = quote, ',
76 # 'dec = dec, fill = fill, comment.char = comment.char).'
77 # )
78 # expect_args(test_mock, 2, FALSE, err)
79 # })
80
81 test_that("empty return list", {
82 m <- mock()
83 expect_null(m())
84 })
85
86
87 test_that("too many calls", {
88 m <- mock(1)
89 expect_equal(1, m())
90 expect_failure(m(), "too many calls to mock object and cycle set to FALSE")
91 })
92
93
94 test_that("return expression", {
95 e <- new.env(parent = globalenv())
96 m <- mock(fun_x("a"),
97 fun_x("a") == "a",
98 envir = e, cycle = TRUE)
99
100 e$fun_x <- function(x)x
101 expect_equal(m(), "a")
102 expect_true(m())
103
104 e$fun_x <- function(x)"abc"
105 expect_equal(m(), "abc")
106 expect_false(m())
107 })
108
109
110 test_that("operator $ works for mock", {
111 m <- mock()
112 expect_equal(mock_calls(m), list())
113 expect_equal(mock_args(m), list())
114 })
115
116
117 test_that("arguments are recorded", {
118 m <- mock()
119 m(x = 1)
120 m(y = 2, z = iris)
121
122 expect_equal(length(m), 2)
123 expect_named(mock_args(m)[[1]], 'x')
124 expect_named(mock_args(m)[[2]], c('y', 'z'))
125
126 expect_equal(mock_args(m)[[1]]$x, 1)
127 expect_equal(mock_args(m)[[2]]$y, 2)
128 expect_equal(mock_args(m)[[2]]$z, iris)
129 })
130
131
132 test_that("expect args", {
133 m <- mock()
134 m(iris)
135 m(x = 1)
136
137 # compares values, not symbols
138 y <- 2
139 z <- iris
140 m(y = y, z = z)
141
142 expect_args(m, 1, iris)
143 expect_args(m, 2, x = 1)
144 expect_args(m, 3, y = 2, z = iris)
145 })
146
147
148 test_that("expect args in with_mock", {
149 m <- mock()
150
151 local_mocked_bindings(lm = m)
152 x <- iris
153 lm(Sepal.Width ~ Sepal.Length, data = x)
154
155 expect_args(m, 1, Sepal.Width ~ Sepal.Length, data = iris)
156 })
157
158
159 test_that("calls are counted", {
160 m <- mock()
161 expect_called(m, 0)
162
163 m()
164 expect_called(m, 1)
165
166 m()
167 expect_called(m, 2)
168 })
169
170
171 test_that("appropriate message if counts are missing", {
172 m <- mock()
173 expect_failure(expect_called(m, 1), "mock object has not been called 1 time")
174 expect_failure(expect_called(m, 2), "mock object has not been called 2 times")
175 })