Skip to content

Commit 0b3fae2

Browse files
committed
bug(test): correctly mock requests
1 parent 2f9464a commit 0b3fae2

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

tests/testthat/test-epidatacall.R

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ test_that("fetch and fetch_tbl", {
88
geo_values = "ca,fl"
99
)
1010

11-
# This test compares the output of a tibble using fetch and fetch_tbl.
12-
with_mocked_bindings(
13-
{
14-
tbl_out <- epidata_call %>% fetch_tbl()
15-
out <- epidata_call %>% fetch()
16-
},
11+
local_mocked_bindings(
12+
request_impl = function(...) NULL,
13+
.package = "epidatr"
14+
)
15+
local_mocked_bindings(
1716
# RDS file generated with
1817
# epidata_call %>%
1918
# fetch_debug(format_type = "classic") %>%
@@ -22,6 +21,8 @@ test_that("fetch and fetch_tbl", {
2221
.package = "httr"
2322
)
2423

24+
tbl_out <- epidata_call %>% fetch_tbl()
25+
out <- epidata_call %>% fetch()
2526
expect_identical(out, tbl_out)
2627
})
2728

@@ -34,20 +35,27 @@ test_that("fetch_tbl warns on non-success", {
3435
time_values = epirange("2020-06-01", "2020-08-01"),
3536
geo_values = "ca,fl"
3637
)
37-
artificial_warning <- "* This is a warning with a leading asterisk and {braces} to make sure we don't have bulleting/glue bugs."
38-
debug_response_content_triplet <-
38+
39+
local_mocked_bindings(
40+
request_impl = function(...) NULL,
41+
.package = "epidatr"
42+
)
43+
local_mocked_bindings(
44+
content = function(...) NULL,
45+
.package = "httr"
46+
)
47+
artificial_warning <- "* This is a warning with a leading asterisk and {braces} to make sure we don't have bulleting/glue bugs."
48+
debug_triplet <- readRDS(testthat::test_path("data/test-classic.rds")) %>%
49+
jsonlite::fromJSON() %>%
50+
`[[<-`("message", artificial_warning)
51+
local_mocked_bindings(
3952
# see generation code above
40-
readRDS(testthat::test_path("data/test-classic.rds")) %>%
41-
jsonlite::fromJSON() %>%
42-
`[[<-`("message", artificial_warning)
43-
with_mocked_bindings(
44-
{
45-
expect_warning(epidata_call %>% fetch_tbl(),
46-
regexp = paste0("epidata warning: ", artificial_warning),
47-
fixed = TRUE
48-
)
49-
},
50-
fromJSON = function(...) debug_response_content_triplet,
53+
fromJSON = function(...) debug_triplet,
5154
.package = "jsonlite"
5255
)
56+
57+
expect_warning(epidata_call %>% fetch_tbl(),
58+
regexp = paste0("epidata warning: ", artificial_warning),
59+
fixed = TRUE
60+
)
5361
})

0 commit comments

Comments
 (0)