Comments (3)
Working on mocking API requests with nealrichardson/httptest. Running into error in basic setup so posting here for reference. Captured requests with httptest::capture_request()
stored in tests/testthat
but then wrapping requests with httptest::with_mock_api()
throws an error
> getwd()
[1] "/Users/sboysel/repos/fredr/tests/testthat"
>
> devtools::load_all(here::here())
Loading fredr
> library(httptest)
> options(httptest.verbose=TRUE)
>
> skip_if_no_key <- function() {
+ testthat::skip_if(
+ condition = identical(Sys.getenv("FRED_API_KEY"), ""),
+ message = "FRED API key is not set."
+ )
+ }
>
> # mock paths
> httptest::.mockPaths()
[1] "."
>
> # capture
> httptest::start_capturing()
> readRenviron(here::here(".Renviron"))
> fredr::fredr_set_key(Sys.getenv("FRED_API_KEY"))
> fredr_category(category_id = 10L)
Writing /Users/sboysel/repos/fredr/tests/testthat/api.stlouisfed.org/fred/category-af4199.json
# A tibble: 1 x 3
id name parent_id
<int> <chr> <int>
1 10 Population, Employment, & Labor Markets 0
> httptest::stop_capturing()
>
> # test with mock api
> httptest::with_mock_api({
+ test_that("fredr_category()", {
+ skip_if_no_key()
+ expect_silent(ctg <- fredr_category(category_id = 10L))
+ })
+ })
Tracing function "form_file" as seen from package "httr"
Error: Test failed: 'fredr_category()'
* `ctg <- fredr_category(category_id = 10L)` produced messages.
Untracing function "form_file" in package "curl"
>
So httptest::.mockPaths()
are default (relative to tests/testthat
). Can't see if I'm missing something fundamental from nealrichardson/httptest docs.
Edit: From the error message, perhaps with_mock_api()
is generating messages causing expect_silent()
to raise an error.
from fredr.
Another complication to testing: switching from httr::GET
to httr::RETRY
in fredr::fredr_request()
introduced some lag in the testing suite given some tests expect failure for bad endpoints. By default, fredr::fredr_request()
will run the request up to 10 times for any failure. I will expose a retry_times
argument in fredr::fredr_request()
, passed directly to the times
argument of internal httr::RETRY
. The default will be 10 but we can reduce the number of tries in expect_error()
tests for which immediate failure is desired (e.g. bad endpoints, malformed requests).
from fredr.
First stab at mocking the FRED API with httptest merged to master with #78
from fredr.
Related Issues (20)
- Unable to Install (Broken Dependency?) HOT 2
- Support for FRED Maps HOT 3
- Option to select Seasonally Adjusted Observation HOT 1
- upper bound too low for limit in fredr_category_series() HOT 3
- Pagination HOT 1
- Encoding error with fredr_series_observations HOT 11
- CRAN issues HOT 25
- Removed from CRAN? HOT 2
- Fredr can't return a legit series_id HOT 3
- Getting fredr back on CRAN HOT 24
- Release fredr 2.0.0
- way to traverse category trees? HOT 4
- This likely isn't an issue with your code, but something to be wary of HOT 3
- Trouble downloading multiple FRED series HOT 5
- fredr() and parameter vintage_dates HOT 5
- M2 not FedFunds
- Release fredr 2.1.0 HOT 1
- fredr appears to be completely broken on my R setup HOT 6
- Capture multiple series simultaneously? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fredr.