I'm trying to write a test that will cover that stop() call, but can't ever get there because "otherpackage" is installed on my computer.
The closest idea I've found is using withr::libpaths() to create a temporary empty library, but I can't get it to actually work. For instance, if I run:
I'd use mocking with testthat::with_mock(). I'm not sure of two things: whether you want/need to use a further package to mock a base R function (not possible with testthat nowadays) and how to mock the results for a function call with arguments so maybe something like
I used to have a similar problem - I was testing for a condition that was certain to be met on my computer or Travis.
At the end I resolved it by including an OR to a logical environment variable in the if() call - and then set it as necessary in my test case. So while the actual condition being tested does not actually happen (in my case no computer is truly offline) the OR triggers the condition and executes the stop / warning call.
network <- as.logical(Sys.getenv("NETWORK_UP", unset = TRUE)) # dummy variable to allow testing of network
if (httr::http_error(remote_file) | !network) {
message('No internet connection or data source broken.')
return(NULL)
}
and in my tests
Sys.setenv("NETWORK_UP" = FALSE)
expect_message(okresy(), "internet") # message, not warning - as per CRAN policy...
Sys.setenv("NETWORK_UP" = TRUE)