Search code examples
ruby-on-railscapybaravcrsystem-testing

How can I use VCR with Rails 5.1 system tests?


Many things on the web seem to suggest that VCR can be used with Capybara.

I have three problems.

  1. This doesn't make much sense to me, because the test driver and the application code don't share memory.
  2. I'm not finding full recipes on how to set this up.
  3. I'm finding bits and pieces of how people have set this up, but it's outside of the context of rails 5.1, which does the capybara setup behind the scenes.

How do I configure a Rails 5.1 app, Capybara, and VCR to work together for system tests?

(My headless browser is phantomjs, driven by poltergeist. But I don't need to intercept requests from the browser, only server-side requests. If I needed to intercept from the browser I would probably use a full http proxy server, like puffing-billy.)


Solution

  • I'm assuming you mean Rails 5.1 since Rails 5 doesn't have system tests.

    1. The copy of the application Capybara runs for testing is run in a separate thread, not a separate process. This means they do have access to the same memory, and loaded classes

    2. There is nothing special required for configuring WebMock or VCR beyond what their READMEs already provide

    3. The setup of Capybara and how Rails handles it is irrelevant to the configuration of WebMock or VCR. Additionally, even when using Rails 5.1 system tests all of the normal Capybara configuration options are still usable.

    That all being said, there are a couple of things to be aware of here. Firstly, WebMock/VCR can only deal with requests made by your app (not from the browser which you stated you don't need) and it's generally better to use faked services (if possible) rather than WebMock/VCR when doing end to end system tests since there is less interference with the code under test.

    If this doesn't answer your issues, post a question with a specific issue you're having, the code that's causing your issue, and the error you're getting.