Skip to content

Commit 27910df

Browse files
committed
Use test_that::local_mocked_bindings()
Rather than changing the global state, even temporary.
1 parent 2ffaa87 commit 27910df

File tree

1 file changed

+3
-20
lines changed

1 file changed

+3
-20
lines changed

tests/testthat/test-java_valid_versions.R

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,11 @@ test_that("fallback is used when the API call fails", {
3838
rJavaEnv.valid_versions_timestamp = NULL
3939
)
4040

41-
fallback <- getOption("rJavaEnv.fallback_valid_versions")
42-
43-
# Backup the original fromJSON function from the jsonlite namespace.
44-
orig_fromJSON <- get("fromJSON", envir = asNamespace("jsonlite"))
45-
46-
# Temporarily override fromJSON to simulate an API failure.
47-
unlockBinding("read_json", asNamespace("jsonlite"))
48-
assign(
49-
"read_json",
50-
function(...) stop("Simulated API failure"),
51-
envir = asNamespace("jsonlite")
52-
)
53-
on.exit(
54-
{
55-
assign("read_json", orig_fromJSON, envir = asNamespace("jsonlite"))
56-
lockBinding("read_json", asNamespace("jsonlite"))
57-
},
58-
add = TRUE
59-
)
41+
local_mocked_bindings(read_json = function(...) stop("Simulated API failure"), .package = "jsonlite")
6042

43+
fallback <- getOption("rJavaEnv.fallback_valid_versions")
6144
versions <- java_valid_versions(force = TRUE)
6245

63-
# When the API call fails, the fallback list should be returned.
46+
## When the API call fails, the fallback list should be returned.
6447
expect_equal(versions, fallback)
6548
})

0 commit comments

Comments
 (0)