I use the testthat library for unit testing in project R. I want to check the code, which depends on the database queries, but not check the answers themselves. In other words, I would like to mock database connections and queries (so that they return a predefined dataset or end up in a test database).
I know that in Ruby there are many gems and other equivalents in other languages ββthat provide this functionality. Is there anything similar for R? Or how do I do this?
some_file.R:
sqlQuery <- function(some_query) { chnl <- odbcConnect(get.db.name()) data <- sqlQuery(chnl, query) }
From the test file:
test_that("test query", { dataset <- sqlQuery("SELECT * FROM some_database_table")
If there are no suitable packages for this, is it better to testthat::with_mock() ?
source share