New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add with_mock_dir #48
Conversation
is there any write-up of how httptest tests work? asking because of my wondering how I'd add one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some notes. For testing, one thought is that you could break this into two cases: one where the directory exists, so we use mocks, and one where the directory does not, so we record. For the former, if you didn't want to cheat and do with_mock_dir(testthat::test_path("."), ...)
you could create a temp dir, copy testthat::test_path("httpbin.org") into that temp dir, and test that you read from that (and that outside of the context, the mockPath is reset).
Testing the recording side is a little trickier if you want to mock what you're recording (🐢 s all the way down) but it's doable. https://github.com/nealrichardson/httptest/blob/master/tests/testthat/test-capture-requests.R uses this capture_while_mocking
helper (https://github.com/nealrichardson/httptest/blob/master/tests/testthat/setup.R#L10), which could be refactored to be used here too. Or you could just have one request, assert that the mock is recorded to the right place, and have it skip_if_disconnected()
.
Keeping those two cases distinct means you can test the use of mocks even if you choose to make the recording test only work with a live connection.
README.md
Outdated
and create mock files under it. | ||
The next times you run it, it will _use_ the mock files in `tests/testthat/httpbin-get`. | ||
To re-record, simply delete the folder. | ||
|
||
**A.** One way to do this is to set `with_mock_api()` to another function in your test file (or in `setup.R` if you want it to run for all test files). So |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would delete the other hacky answers, they're no longer recommended if this function exists IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the hacky answers are for a different use case though? When you want to turn off using httptest entirely? à la VCR_OFF (an environment variable in vcr).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which I would actually like to recommend in the book, run tests against the real service with a scheduled CI run.
This comment has been minimized.
This comment has been minimized.
Codecov Report
@@ Coverage Diff @@
## master #48 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 18 +1
Lines 447 451 +4
=========================================
+ Hits 447 451 +4
Continue to review full report at Codecov.
|
(thanks for the notes on testing!) |
R/mock-dir.R
Outdated
#' | ||
with_mock_dir <- function(dir, expr, simplify = TRUE) { | ||
|
||
if (!nzchar(Sys.getenv("TESTING_MOCK_DIR"))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the most elegant thing ever... but otherwise things get messy when testing this function. I had no better idea.
README.md
Outdated
and create mock files under it. | ||
The next times you run it, it will _use_ the mock files in `tests/testthat/httpbin-get`. | ||
To re-record, simply delete the folder. | ||
|
||
#### How do I switch between mocking and real requests? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is still an important question.
I'll take a look, and if I can come up with anything better for testing, I'll push a commit to your branch. |
I simplified the code a bit and likely broke the interactive use case, but I think that needs a more general solution (#50). I'll work on that next. The other thing to do here is make a pass over the docs and make sure |
R/mock-dir.R
Outdated
#' @export | ||
#' | ||
with_mock_dir <- function (dir, expr, simplify=TRUE) { | ||
with_mock_path(dir, replace=TRUE, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did wonder whether there was a use case where replace=FALSE
would make sense (i.e. whether replace
should go in the signature of with_mock_dir
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah! Good question, but better to add it to the signature then.
Thanks for the review & improvements! |
vignettes/httptest.Rmd
Outdated
To re-record, simply delete the folder. | ||
|
||
Now, this has the downside of creating one mock directory per test. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you add a sentence or two about the other ways to get some sort of automatic recording? Or link to an FAQ? I can't remember where you documented the use of a environment variable in setup.R to replace with_mock_api()
with capture_requests()
.
Fix #40