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

support looping through responses for the same request? #190

Closed
maelle opened this issue Nov 16, 2020 · 5 comments
Closed

support looping through responses for the same request? #190

maelle opened this issue Nov 16, 2020 · 5 comments

Comments

@maelle
Copy link
Member

maelle commented Nov 16, 2020

Context: At the moment for faking some behavior of the server (e.g. an error, or simpler responses to decrease the fixture size) one needs to use webmockr.
This is not ideal as it's quite different from vcr.

One alternative is

  • write your test and use_cassette() around the code calling the API.
  • run the test
  • edit the cassette by hand

It's quite handy IMHO.

Now, one thing that this workflow can't handle is testing retries. So I wonder whether there'd be a way to add a field "count" to the fixture and e.g. have

http_interactions:
- request:
    count: 1
    method: get
    uri: https://kctbh9vrtdwd.statuspage.io/api/v2/components.json
    body:
      encoding: ''
      string: ''
    headers:
      Accept: application/json, text/xml, application/xml, */*
  response:
    - count: 1
      status_code: 502
    - count: 2
      status_code: 200

and vcr would loop through the responses?

@sckott
Copy link
Collaborator

sckott commented Nov 16, 2020

vcr has something that may address this already, see e.g., in the Ruby vcr docs https://relishapp.com/vcr/vcr/v/5-1-0/docs/request-matching/playback-repeats

and we have that arg in this pkg https://docs.ropensci.org/vcr/reference/use_cassette.html - I've not used it yet, so can't be 100% certain it works as intended, so would be good to try

@maelle
Copy link
Member Author

maelle commented Nov 17, 2020

Oh actually the default behavior is the right one, awesome.

See the test of gh_organizations() in ropensci-books/exemplighratia#8, I think it fails once and then succeeds.

So I'm a bit undecided regarding what to encourage folks to use

  • webmockr
  • fake cassettes

Maybe the book should just mention both. webmockr allows more flexibility in how you define a sequence, but it's a new thing to learn after making the effort to understand vcr and cassettes. 🤔

@sckott
Copy link
Collaborator

sckott commented Nov 17, 2020

Oh actually the default behavior is the right one

of? do you mean the default value for allow_playback_repeats parameter?

@maelle
Copy link
Member Author

maelle commented Nov 17, 2020

Yes, I didn't have to change anything

@maelle
Copy link
Member Author

maelle commented Nov 26, 2020

@maelle maelle closed this as completed Nov 26, 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

No branches or pull requests

2 participants