When creating database fixtures, it can sometimes be helpful to see record the responses from the database for use in crafting tests.

start_db_capturing(path, redact_columns = NULL)

start_capturing(path, redact_columns = NULL)





the path to record mocks (default if missing: the first path in .db_mock_paths().


a character vector of columns to redact. Any column that matches an entry will be redacted with a standard value for the column type (e.g. characters will be replaced with "[redacted]")


NULL (invisibily)


You can start capturing with start_db_capturing() and end it with stop_db_capturing. All queries run against a database will be executed like normal, but their responses will be saved to the mock path given, so that if you use the same queries later inside of a with_mock_db block, the database functions will return as if they had been run against the database.

You can redact certain columns using the redact_columns argument. This will replace the values in the column with a generic redacted version. This works by always passing the data being saved through redact_columns().

note You should always call DBI::dbConnect inside of the capturing block. When you connect to the database, dittodb sets up the mocks for the specific database you're connecting to when you call DBI::dbConnect.

start_capturing() and stop_capturing() do the exact same thing as start_db_capturing() and stop_db_capturing(). They are both deprecated so as not to clash with other packages when loaded.


if (FALSE) { start_db_capturing() con <- dbConnect(RSQLite::SQLite(), "memory") df_1 <- dbGetQuery(con, "SELECT * FROM rpostgresql.airlines LIMIT 1") res <- dbSendQuery(con, "SELECT * FROM rpostgresql.airlines LIMIT 2") df_2 <- dbFetch(res) dbDisconnect(con) stop_db_capturing() start_db_capturing(redact_columns = "carrier") con <- dbConnect(RSQLite::SQLite(), "memory") df_1 <- dbGetQuery(con, "SELECT * FROM rpostgresql.airlines LIMIT 3") dbDisconnect(con) stop_db_capturing() }