Skip to content
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

Merged
merged 23 commits into from Dec 11, 2020
Merged

add with_mock_dir #48

merged 23 commits into from Dec 11, 2020

Conversation

maelle
Copy link
Contributor

@maelle maelle commented Oct 22, 2020

Fix #40

R/mock-dir.R Outdated Show resolved Hide resolved
@maelle
Copy link
Contributor Author

maelle commented Oct 22, 2020

is there any write-up of how httptest tests work? asking because of my wondering how I'd add one.

Copy link
Owner

@nealrichardson nealrichardson left a 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.

R/mock-dir.R Outdated Show resolved Hide resolved
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
Copy link
Owner

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

Copy link
Contributor Author

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).

Copy link
Contributor Author

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.

R/mock-dir.R Outdated Show resolved Hide resolved
R/mock-dir.R Outdated Show resolved Hide resolved
@maelle

This comment has been minimized.

@codecov-io
Copy link

codecov-io commented Oct 23, 2020

Codecov Report

Merging #48 (f2fa8cd) into master (541a48c) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #48   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           17        18    +1     
  Lines          447       451    +4     
=========================================
+ Hits           447       451    +4     
Impacted Files Coverage Δ
R/mock-dir.R 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 541a48c...f2fa8cd. Read the comment docs.

@maelle maelle marked this pull request as ready for review November 9, 2020 13:29
@maelle
Copy link
Contributor Author

maelle commented Nov 9, 2020

(thanks for the notes on testing!)

@maelle maelle marked this pull request as draft November 9, 2020 13:31
@maelle maelle marked this pull request as ready for review November 9, 2020 13:34
R/mock-dir.R Outdated
#'
with_mock_dir <- function(dir, expr, simplify = TRUE) {

if (!nzchar(Sys.getenv("TESTING_MOCK_DIR"))) {
Copy link
Contributor Author

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?
Copy link
Contributor Author

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.

@nealrichardson
Copy link
Owner

I'll take a look, and if I can come up with anything better for testing, I'll push a commit to your branch.

@nealrichardson
Copy link
Owner

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 with_mock_dir is discussed everywhere it should be.

R/mock-dir.R Outdated
#' @export
#'
with_mock_dir <- function (dir, expr, simplify=TRUE) {
with_mock_path(dir, replace=TRUE, {
Copy link
Owner

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).

Copy link
Contributor Author

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.

@maelle
Copy link
Contributor Author

maelle commented Nov 25, 2020

Thanks for the review & improvements!

To re-record, simply delete the folder.

Now, this has the downside of creating one mock directory per test.

Copy link
Contributor Author

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().

@nealrichardson nealrichardson mentioned this pull request Nov 25, 2020
18 tasks
@nealrichardson nealrichardson merged commit 968801a into nealrichardson:master Dec 11, 2020
nealrichardson pushed a commit that referenced this pull request Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

General way to turn mocking off?
3 participants