Search code examples
ruby-on-railscapybararackmiddlewarelivereload

How can I add Rack Middleware to the test environment in Rails?


Situation

I am currently testing a Rails app using Capybara. In addition, I was using Guard with its extension guard-livereload to automatically reload my browser as soon as relevant source files changed.

As the save_and_open_page method from Capybara did not display stylesheets correctly, I applied this solution to the problem, in which a temporary view-dump capybara.html gets placed in the /public/ folder to ensure the accessibility of assets.

Now, as LiveReload has worked like a charm in development, I would like to use it during feature-tests to automatically reload /public/capybara.html instead of opening it over and over myself.

Problem

For some reason I can only insert the Rack Middleware, which is responsible for reloading the page, into the middleware-stack within the developent-environment, but not within the test-environment. I use the following code for insertion:

/config/environments/development.rb

Rails.application.configure do

  config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload

end

When using the same method in /config/environments/test.rb, the following error occurs

myApp/config/environments/test.rb:44:in `block in <top (required)>': uninitialized constant Rack::LiveReload (NameError)

As I am still rather new to Rails, I don't really know where to start here. As far as I know, trying to require the file manually wouldn't really be The Rails WayTM . So, how can I resolve this problem?

Thanks in advance.


Solution

  • In your Gemfile you are probably only loading the rack-livereload gem in the development group -- for this to work you would need to be loading it in the development and test groups. That being said, you really want your test environment to mimic production as closely as possible, so running rack-livereload in the test environment would usually be a bad idea.