Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Replace with_mock() with with_mocked_bindings() #970

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tests/testthat/test-parse-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ test_that("filter passes on content-type", {
body = "this is a body",
HTTP_CONTENT_TYPE = "text/html; charset=testset",
)
with_mock(
local_mocked_bindings(
parse_body = function(body, content_type = "unknown", parsers = NULL) {
print(content_type)
body
},
{
expect_output(req_body_parser(req, make_parser("text")), "text/html; charset=testset")
},
.env = "plumber"
}
)
expect_output(
req_body_parser(req, make_parser("text")),
"text/html; charset=testset"
)
})

Expand Down
51 changes: 24 additions & 27 deletions tests/testthat/test-plumber-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,37 +100,34 @@ test_that("`debug` is not set until runtime", {
skip_if_not_installed("mockery", "0.4.2")

m <- mockery::mock(TRUE, cycle = TRUE)
# https://github.com/r-lib/testthat/issues/734#issuecomment-377367516
# > It should work if you fully qualify the function name (include the pkgname)
with_mock("plumber:::default_debug" = m, {
root <- pr()
root$getDebug()
mockery::expect_called(m, 1)
local_mocked_bindings(default_debug = m)

with_interrupt({
root %>% pr_run(quiet = TRUE)
})
# increase by 1
mockery::expect_called(m, 2)
root <- pr()
root$getDebug()
mockery::expect_called(m, 1)

# listen to set value
with_interrupt({
root %>%
pr_set_debug(TRUE) %>%
pr_run(quiet = TRUE)
})
# not updated. stay at 2
mockery::expect_called(m, 2)
with_interrupt({
root %>% pr_run(quiet = TRUE)
})
# increase by 1
mockery::expect_called(m, 2)

# listen to run value
with_interrupt({
root %>%
pr_run(debug = FALSE, quiet = TRUE)
})
# not updated. stay at 2
mockery::expect_called(m, 2)
# listen to set value
with_interrupt({
root %>%
pr_set_debug(TRUE) %>%
pr_run(quiet = TRUE)
})
# not updated. stay at 2
mockery::expect_called(m, 2)

# TODO test that run(debug=) has preference over pr_set_debug()
# listen to run value
with_interrupt({
root %>%
pr_run(debug = FALSE, quiet = TRUE)
})
# not updated. stay at 2
mockery::expect_called(m, 2)

# TODO test that run(debug=) has preference over pr_set_debug()
})
Loading