0

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.)

John Bachir
  • 22,495
  • 29
  • 154
  • 227

1 Answers1

0

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.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Hi Thomas! I did a test in which I set a global variable in the beginning of a system test, and then printed it out in the controller which it was invoking. the global variable was `nil`. from this i concluded they don't share memory. I also tried using VCR (as I have on a half-dozen projects over the last 5 years) and it didn't work. I'll post another question with my specific VCR setup, but any thoughts on my shared memory test? – John Bachir May 25 '17 at 23:16
  • @JohnBachir If it's truly global and puma rack handler isn't starting a separate process then I would think it should be visible -- what output is puma producing when it starts up? – Thomas Walpole May 25 '17 at 23:19
  • Here's the puma output when it starts up: https://gist.github.com/jjb/34a393ef528d67ef43e064638a6c86d8 – John Bachir May 25 '17 at 23:28
  • 1
    @JohnBachir And there's your issue -- "starting in cluster mode" -- it's opening multiple processes (and running them in development env rather than test) rather than running in process – Thomas Walpole May 25 '17 at 23:29
  • ‍♂️ huh. Okay I will explore fixing that. – John Bachir May 25 '17 at 23:35
  • @JohnBachir From the other thread - I would guess you're running into an issue where puma will read from any of a number of config files that are in your project and one of them is setting cluster mode – Thomas Walpole May 25 '17 at 23:36
  • @JohnBachir 2 things to try and see if it is a config file issue - add a config_files: ['-'] option to your puma server registration, or try creating an empty config/puma/test.rb file in your project and see if that makes a difference – Thomas Walpole May 25 '17 at 23:44
  • At least part of my problem is that I'm using the heroku puma plugin, which reads RACK_ENV instead of RAILS_ENV, and always preloads even for 1 worker. https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb – John Bachir May 25 '17 at 23:51
  • @JohnBachir Try not including that gem in the test group in your gemfile – Thomas Walpole May 25 '17 at 23:54
  • Could do that, but it's nice to have just one puma config and generally keep config simple and mostly driven by ENV variables. These two PRs should help: https://github.com/puma/puma-heroku/pull/6 https://github.com/puma/puma-heroku/pull/7 – John Bachir May 26 '17 at 00:25
  • @JohnBachir FTI - in https://github.com/puma/puma-heroku/pulls -- the worker count needs to be set to 0 for puma to run in single mode – Thomas Walpole May 26 '17 at 05:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145234/discussion-between-john-bachir-and-thomas-walpole). – John Bachir May 26 '17 at 19:21