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

Allow mocking of any object #1909

Merged
merged 2 commits into from
Nov 29, 2023
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# testthat (development version)

* `local_mocked_bindings()` can now mock any object, not just functions
(#1896).

* `skip_if_offline()` now uses `captive.apple.com` by default. This is the
host that Apple devices use to check that they're online so should be
a higher reliability host than `r-project.org` (@jdblischak, #1890).
Expand Down
13 changes: 4 additions & 9 deletions R/mock2.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
#' )
#' ```
#' @export
#' @param ... Name-value pairs providing functions to mock.
#' @param ... Name-value pairs providing new values (typically functions) to
#' temporarily replace the named bindings.
#' @param code Code to execute with specified bindings.
#' @param .env Environment that defines effect scope. For expert use only.
#' @param .package The name of the package where mocked functions should be
Expand Down Expand Up @@ -195,14 +196,6 @@ check_bindings <- function(x, error_call = caller_env()) {
call = error_call
)
}

is_fun <- map_lgl(x, is.function)
if (!any(is_fun)) {
cli::cli_abort(
"All elements of {.arg ...} must be functions.",
call = error_call
)
}
}

# For testing -------------------------------------------------------------
Expand Down Expand Up @@ -244,6 +237,8 @@ show_bindings <- function(name, env = caller_env()) {
invisible()
}

test_mock_value <- 10

env_desc <- function(env) {
cat(obj_address(env), ": ", env_name(env), "\n", sep = "")
}
3 changes: 2 additions & 1 deletion man/local_mocked_bindings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions tests/testthat/_snaps/mock2.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
Condition
Error in `local_mocked_bindings()`:
! All elements of `...` must be named.
Code
with_mocked_bindings(1 + 1, x = 2)
Condition
Error in `local_mocked_bindings()`:
! All elements of `...` must be functions.

# can't mock bindings that don't exist

Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-mock2.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ test_that("can make wrapper", {
test_that("with_mocked_bindings() validates its inputs", {
expect_snapshot(error = TRUE, {
with_mocked_bindings(1 + 1, function() 2)
with_mocked_bindings(1 + 1, x = 2)
})
})

Expand Down Expand Up @@ -81,3 +80,8 @@ test_that("can mock base functions with in-package bindings", {
local_mocked_bindings(interactive = function() TRUE)
expect_equal(test_mock_base(), TRUE)
})

test_that("can mock values", {
local_mocked_bindings(test_mock_value = 100)
expect_equal(test_mock_value, 100)
})
Loading