I'm using testthat to write unit tests for a package. In one part of my package, I include a check to make sure a different package is installed:
if (!requireNamespace("otherpackage", quietly = TRUE)) {
stop("Package \"otherpackage\" isn't installed", call. = FALSE)
}
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:
withr::with_temp_libpaths({
installed.packages()
})
…I still get a list of all the packages installed on my computer, not an empty environment.
Is there a way to make testthat think that I don't have the package installed?