Skip to content

Commit 1cdd58d

Browse files
authored
Merge pull request #207 from maelle/mock
test: use function factory instead of {mockery}
2 parents cb0954e + 9b274d6 commit 1cdd58d

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

DESCRIPTION

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Imports:
4242
Suggests:
4343
devtools,
4444
knitr,
45-
mockery,
4645
rmarkdown,
4746
testthat (>= 3.0.0),
4847
withr

tests/testthat/helper_mock.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# the function below is a function factory, a function that creates and returns
2+
# another function
3+
4+
# it create a mock object that returns user inputs in sequence
5+
6+
# outer function initialises the index. inner function increments counter i
7+
# every time it is called and returns i-th element in the responses vector
8+
9+
# The <<- operator is used to modify the i variable in the parent environment
10+
# (the environment of helper_mock)
11+
12+
# new_mock <- helper_mock("3", "This is a note", "n")
13+
# new_mock() # [1] "3"
14+
# new_mock() # [1] "This is a note"
15+
# new_mock() # [1] "n"
16+
# new_mock() # [1] NA
17+
# new_mock() # [1] NA
18+
19+
helper_mock <- function(...) {
20+
responses <- c(...)
21+
i <- 0
22+
function(...) {
23+
i <<- i + 1
24+
responses[i]
25+
}
26+
}

tests/testthat/test-user_categorisation.R

+15-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
test_that("user_categorisation works with valid input", {
2-
# create a mock object that returns user inputs in sequence
3-
mock_readline <- mockery::mock("3", "This is a note", "n")
2+
43
# replace `readline` function within the `user_categorisation` function with
5-
# the `mock_readline` mock object
6-
mockery::stub(user_categorisation, "readline", mock_readline)
4+
# the `helper-mock_factory.R` mock object
5+
local_mocked_bindings(
6+
readline = helper_mock("3", "This is a note", "n")
7+
)
78

89
response <- user_categorisation(var = "Variable1",
910
desc = "Description1",
@@ -12,11 +13,9 @@ test_that("user_categorisation works with valid input", {
1213
})
1314

1415
test_that("user_categorisation handles invalid input and then valid input", {
15-
# create a mock object that returns invalid input first, then valid input
16-
mock_readline <- mockery::mock("6", "3", "This is a note", "n")
17-
# replace `readline` function within the `user_categorisation` function with
18-
# the `mock_readline` mock object
19-
mockery::stub(user_categorisation, "readline", mock_readline)
16+
local_mocked_bindings(
17+
readline = helper_mock("6", "3", "This is a note", "n")
18+
)
2019

2120
response <- user_categorisation(var = "Variable1",
2221
desc = "Description1",
@@ -25,12 +24,9 @@ test_that("user_categorisation handles invalid input and then valid input", {
2524
})
2625

2726
test_that("user_categorisation handles multiple valid inputs", {
28-
# create a mock object that returns multiple valid inputs
29-
mock_readline <- mockery::mock("3,4", "This is another note", "n")
30-
# replace `readline` function within the `user_categorisation` function with
31-
# the `mock_readline` mock object
32-
mockery::stub(user_categorisation, "readline", mock_readline)
33-
27+
local_mocked_bindings(
28+
readline = helper_mock("3,4", "This is another note", "n")
29+
)
3430
response <- user_categorisation(var = "Variable1",
3531
desc = "Description1",
3632
type = "Type1", domain_code_max = 5)
@@ -39,12 +35,10 @@ test_that("user_categorisation handles multiple valid inputs", {
3935
})
4036

4137
test_that("user_categorisation handles re-do input", {
42-
# create a mock object that returns inputs including re-do
43-
mock_readline <- mockery::mock("3", "This is a note", "y", "4",
44-
"Another note", "n")
45-
# replace `readline` function within the `user_categorisation` function with
46-
# the `mock_readline` mock object
47-
mockery::stub(user_categorisation, "readline", mock_readline)
38+
local_mocked_bindings(
39+
readline = helper_mock("3", "This is a note", "y", "4",
40+
"Another note", "n")
41+
)
4842

4943
response <- user_categorisation(var = "Variable1",
5044
desc = "Description1",

0 commit comments

Comments
 (0)