"Mocking"/Handling external http requests #106
Replies: 3 comments
-
using ruby VCR is something that has been attempted https://github.com/shakacode/cypress-on-rails/tree/3a5e5a2f13bb2961b15d59a8010ff0f44501e4ad#for-vcr This approach attempts to enable VCR in a request and disable it in another request and unfortunately has short coming as VCR does not like to be disabled in another thread so you have you have run server with a single process and single thread. Also if the second request to disable it does not run (because of a test failure), you are left with VCR still enabled for the next test. The approach of #93 enables and disables VCR per request, essentially wrapping each request in it's own VCR cassette. This at least better handles not leaving VCR enabled but suffers from race conditions as multiple VCRs might want to open/write to the same file which starting the server in single process and thread mode could mitigate. |
Beta Was this translation helpful? Give feedback.
-
One Approach we are currently implementing is to implement 2 versions of a service, the "real" service and a "fake" service. Code example: module CatFactsService
def self.new_service
ENV['CYPRESS'] ? Fake.new : Real.new
end
class Real
def facts
# real api code here
end
end
class Fake
def facts
File.read("#{__dir__}/cat_facts_sample.json")
end
end
end And use it like this in the code. cat_service = CatFactsService.new_service
cat_facts = cat_service.facts Currently this gives us the best flexibility, as you can implement the "fake" service anyway you like (even a VCR implementation that wraps the Real service). |
Beta Was this translation helpful? Give feedback.
-
Thanks @grantspeelman, sorry it took me ages to reply. We're doing the same thing with some services stubbing them out in cypress. Currently we had to turn off single-threading puma(We had some code for inlining styles that needed to be done in another thread) in cypress so we're back to some flakyness. I remember the stubs were still giving us some flakyness or they weren't considered at all in some requests, so we are using the the vcr implementation on a test that does a lot of requests and it seems to work better. I'd say this module stubs should be in the docs or in a wiki somewhere as an alternative. |
Beta Was this translation helpful? Give feedback.
-
Currently a difficult thing with end to end tests is handling 3rd party http requests. I am starting this discussion as a place to share how people are currently working around this and discuss possible solutions on how we could possible solve and get it built in the gem.
Beta Was this translation helpful? Give feedback.
All reactions