Using vcr for unit testing
vcr supports use with the testthat package. Here's the steps to follow:
First, note that vcr only works with a single HTTP client for now: crul
In addition to testthat, add vcr and webmockr to your Suggests in your DESCRIPTION file
Add a file (named e.g., vcr-config.R
) to your tests/testthat/
directory. In that file add your vcr configuration settings.
See vcr_configure for help on configuration settings.
For any given test use the following:
use_cassette("foobar", { aa <- hello::world() expect_is(aa, "SomeClass") expect_equal(length(aa), 3) })
And the tests will behave as normally.
The first request will make a real HTTP request. Following requests will pull from the cached responses on cassette.