r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
868 stars 313 forks source link

local_mocked_bindings shows different behaviour devtools::test() vs command line , github, devtools::check #1940

Closed shug0131 closed 2 months ago

shug0131 commented 3 months ago

Using local_mocked_bindings() helped me to finally to solve a thorny testing of a function that used utils::askYesNo. So very impressed overall.

But, I found that it worked as expected in most cases (running on command line an individual test, using devtools::check, on a github action with covr::package_coverage) .

But when I used devtools::test(), it would still show the dialogue and need me to hit return. More curiously, even though I ran two tests (for when I say "Yes" and "No"), the dialogue only appeared once, and I only hit return once.

This might actually be desirable behaviour, but needs some more documentation and explanation.

hadley commented 2 months ago

Could you provide more details and/or a reprex?

shug0131 commented 2 months ago

I just tried to reproduce with

dialogue <- function(){
answer=askYesNo(
  msg="Is R better than SAS?"
)
return(answer)
}

and

test_that("reprex for dialogue",{
 my_askYesNo_mock= function(...){TRUE}
 local_mocked_bindings(askYesNo = my_askYesNo_mock)
  expect_true( dialogue() )
}
)

But now devtools::test(), works the same as all the other ways of running the test, with no dialogue or interaction needed.

Only explanation I can suggest is me using an updated version of R ( 4.3.2, with CRAN packages taken from https://p3m.dev/cran/__linux__/jammy/2024-02-28)

Happy to consider this as resolved.