-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
with_mock not working when inside test_that function #734
Comments
Does it work if you put |
actually, if I run in the console: add_one <- function(x) x + 1
test_that("lala", {
expect_equal(add_one(2), 3)
f <- function() {}
with_mock(
add_one = function(x) x - 1,
expect_equal(add_one(2), 1)
)
}) it works. |
This is an expected behaviour. Test that is trying to find function inside the package if test_package() is called: env <- asNamespace(pkg_name)
if (!exists(name, envir = env, mode = "function"))
stop("Function ", name, " not found in environment ",
environmentName(env), ".", call. = FALSE) If you want to test functions outside the package , you might try calling |
So now I have the function I want to mock inside the R folder of my package. When only I execute the |
@hannaET I cannot reproduce that using your code. I have tried it with:
|
It should work if you fully qualify the function name (include the pkgname). e.g. with_mock(
"pkgname:::add_one" = function() |
- r-lib/testthat#734 - Need to specify the package name regmedint::: - These are added as examples of how to check which functions are being called.
I suggest to add a note to the documentation for the requirement to fully qualify the function when used with |
I can't get the with_mock example to work when it is inside a test_that.
I tested it in a fresh R session (R version 3.4.3, testthat version 2.0.0)
If someone could help me on that, it would be great.
The text was updated successfully, but these errors were encountered: