nealrichardson / httptest

A Test Environment for HTTP Requests in R
https://enpiar.com/r/httptest/
Other
79 stars 10 forks source link

Option to provide path to the json files with with_mock_api #87

Open condwanaland opened 10 months ago

condwanaland commented 10 months ago

FYI, I have cross-posted a version of this question to Stack Overflow

I've used capture_requests to record the output of an API call my package makes

capture_requests({
  download_scrobbles(.limit1 = TRUE)
})

In this example the API username and password are loaded via env variables

Sys.getenv('LASTFM_API_USERNAME'); Sys.getenv('LASTFM_API_KEY')

However, when I come to write the test

with_mock_api(
  {
    test_that("Output is producted correctly", {
      local_edition(3)
      expect_snapshot_value(
        download_scrobbles(.limit1 = TRUE),
        style = "deparse")
    })
  }
)

This fails as testthat can't access my .Renviron. I also couldn't just pass the values directly because then they would be checked into git via the test_ file

My intial idea was just to supply fake credentials as the API doesn't actually need to be accessed

with_mock_api(
  {
    test_that("Output is producted correctly", {
      local_edition(3)
      expect_snapshot_value(
        download_scrobbles("fake_user", "fake_key", .limit1 = TRUE),
        style = "deparse")
    })
  }
)

However, this causes httptest to think it doesn't have any requests recorded as it uses the url to build the path and these params get passed in to the GET url.

Maybe my FR here is to be able to override with_mock_api finding the json files automatically and just to be able to supply them directly? The data is there and works great, it's just a matter of pointing to the right path