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

with_mock appears not to work #5

Closed
gvwilson opened this issue Nov 27, 2018 · 3 comments · Fixed by #20
Closed

with_mock appears not to work #5

gvwilson opened this issue Nov 27, 2018 · 3 comments · Fixed by #20

Comments

@gvwilson
Copy link

mockr::with_mock appears not to work in this case:

library(mockr)

realFunction <- function() {
  "real value"
}

functionToTest <- function(title) {
  result <- realFunction()
  cat(title, "result is", result, "\n")
}

functionToTest("without mock:")

replacementFunction <- function() {
  "replacement value"
}

with_mock(
  realFunction = replacementFunction,
  functionToTest("mocking with replacementFunction:")
)

with_mock(
  realFunction = function() "inline value",
  functionToTest("mocking with inline replacement")
)

output:

without mock: result is real value 
mocking with replacementFunction: result is real value 
mocking with inline replacement result is real value

If I replace library(mockr) with library(testthat) and re-run without any other changes, the output is as expected:

without mock: result is real value 
mocking with replacementFunction: result is replacement value 
mocking with inline replacement result is inline value

I am using mockr version 0.1 (from install.packages("mockr")) and testthat 2.0.0.9000 with R version 3.5.1 on Mac OS X 10.13.6 (High Sierra).

@krlmlr
Copy link
Owner

krlmlr commented Dec 11, 2020

Thanks. This is by design, see the last example in the README. How can we document this better?

@krlmlr
Copy link
Owner

krlmlr commented Dec 18, 2020

With the pull request:

library(mockr)

realFunction <- function() {
  "real value"
}

functionToTest <- function(title) {
  result <- realFunction()
  cat(title, "result is", result, "\n")
}

functionToTest("without mock:")
#> without mock: result is real value

replacementFunction <- function() {
  "replacement value"
}

with_mock(
  realFunction = replacementFunction,
  functionToTest("mocking with replacementFunction:")
)
#> Warning: Replacing functions in evaluation environment: `realFunction()`
#> mocking with replacementFunction: result is replacement value

with_mock(
  realFunction = function() "inline value",
  functionToTest("mocking with inline replacement:")
)
#> Warning: Replacing functions in evaluation environment: `realFunction()`
#> mocking with inline replacement: result is inline value

Created on 2020-12-18 by the reprex package (v0.3.0)

krlmlr added a commit that referenced this issue Dec 18, 2020
- Functions declared in evaluation environments are now also replaced, with a warning (#5).
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants