-
Notifications
You must be signed in to change notification settings - Fork 17
/
16-gotchas.Rmd
56 lines (44 loc) · 2.82 KB
/
16-gotchas.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
```{r echo = FALSE}
knitr::opts_chunk$set(
comment = "#>",
warning = FALSE,
message = FALSE
)
```
# Gotchas {#gotchas}
There's a few things to watch out for when using `vcr`.
- **Security**: Don't put your secure API keys, tokens, etc. on the public web. See the [Security chapter](#security-chapter) and the [vcr security chapter](#vcr-security).
- **API key issues**: Running vcr enabled tests in different contexts when API keys are used can have some rough edges.
- **Dates**: Be careful when using dates in tests with `vcr`. e.g. if you generate todays date, and pass that in to a function in your package that uses that date for an HTTP request, the date will be different from the one in the matching cassette, causing a `vcr` failure.
- **HTTP errors**: It's a good idea to test failure behavior of a web service in your test suite. Sometimes `vcr` can handle that and sometimes it cannot. Open any issues about this because ideally i think `vcr` could handle all cases of HTTP failures.
- **Very large response bodies**: A few things about large response bodies. First, `vcr` may give you trouble with very large response bodies as we've see yaml parsing problems already. Second, large response bodies means large cassettes on disk - so just be aware of the file size if that's something that matters to you. Third, large response bodies will take longer to load into R, so you may still have a multi second test run even though the test is using a cached HTTP response.
- **Encoding**: We haven't dealt with encoding much yet at all, so we're likely to run into encoding issues. One blunt instrument for this for now is to set `preserve_exact_body_bytes = TRUE` when running `vcr::use_cassette()` or `vcr::insert_cassette()`, which stores the response body as base64.
- **devtools::check vs. devtools::test**: See (\@ref(check-vs-test))
- **ignored files**: See (\@ref(managing-cassettes))
## Correct line identification {#line-identification}
To get the actual lines where failures occur, you can wrap the `test_that` block in a `use_cassette()` block:
```r
library(testthat)
vcr::use_cassette("rl_citation", {
test_that("my test", {
aa <- rl_citation()
expect_is(aa, "character")
expect_match(aa, "IUCN")
expect_match(aa, "www.iucnredlist.org")
})
})
```
OR put the `use_cassette()` block on the inside, but make sure to put `testthat` expectations outside of
the `use_cassette()` block:
```r
library(testthat)
test_that("my test", {
vcr::use_cassette("rl_citation", {
aa <- rl_citation()
})
expect_is(aa, "character")
expect_match(aa, "IUCN")
expect_match(aa, "www.iucnredlist.org")
})
```
Do not wrap the `use_cassette()` block inside your `test_that()` block with `testthat` expectations inside the `use_cassette()` block, as you'll only get the line number that the `use_cassette()` block starts on on failures.