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

Re-enter the mocking game #1739

Merged
merged 12 commits into from
Mar 1, 2023
Merged

Re-enter the mocking game #1739

merged 12 commits into from
Mar 1, 2023

Conversation

hadley
Copy link
Member

@hadley hadley commented Feb 12, 2023

No description provided.

Copy link
Member

@gaborcsardi gaborcsardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
Copy link
Member

@lionel- lionel- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorporating what we've learned from mockr and mockery

So what did we learn? I'm curious as I have never used these packages.

R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
R/mock2.R Outdated Show resolved Hide resolved
@hadley
Copy link
Member Author

hadley commented Feb 13, 2023

What we learned:

  • We now have the knowledge and tools to radically simplify the implementation. Compare with_mocked_bindings() to with_mock().
  • Now that I've more carefully inspected the implementation of mockery and mockr, I think it actually uses a new technique, and this now seems like "obvious" technique for it to use.
  • It's really important for the mocking tool to work in both batch test mode and interactively.

@hadley
Copy link
Member Author

hadley commented Feb 16, 2023

Note that this doesn't appear to work to mock S3 methods.

# In package
mockable_generic <- function(x) {
  UseMethod("mockable_generic")
}
#' @export
mockable_generic.integer <- function(x) {
  x + 1
}

# In test

test_that("local_mocked_bindings affects S3 methods", {
  local_mocked_bindings(mockable_generic.integer = function(x) 100)
  expect_equal(mockable_generic(1L), 100)
})

I tried also binding in the S3 methods table:

  method_env <- ns_env[[".__S3MethodsTable__."]]
  methods <- names(bindings) %in% names(method_env)
  local_bindings(!!!bindings[methods], .env = method_env, .frame = .env)
  bindings <- bindings[!methods]

But while that works when called interactively, it doesn't work in tests.

@lionel-
Copy link
Member

lionel- commented Feb 16, 2023

For S3 methods it is usually sufficient to bind them in the global environment.

Edit: Though the S3 table might have precedence if existing methods are registered.

@hadley
Copy link
Member Author

hadley commented Feb 16, 2023

@lionel- yeah, that only works for new methods, I think.

But maybe my attempt didn't work because of something to do with the way that testthat sets up environments. It certainly looks like pkgload modifies the same place when unloading packages: https://github.com/r-lib/pkgload/blob/main/R/unload.R#L157-L164. (However, that does suggest that, in general, I need to look in the namespace of the generic, not just the namespace of the current package.)

@hadley
Copy link
Member Author

hadley commented Feb 16, 2023

Oooooh, I wonder if it's because test_env() does env_clone(asNamespace(package)).

R/mock2.R Outdated Show resolved Hide resolved
@hadley hadley marked this pull request as ready for review March 1, 2023 13:18
@hadley hadley merged commit 253c882 into main Mar 1, 2023
@hadley hadley deleted the mocking branch March 1, 2023 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants