简体   繁体   中英

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 Way TM . So, how can I resolve this problem?

Thanks in advance.

I believe you should include the Rack::LiveReload in your test environment inside Gemfile :

group :development, :test do
  gem "rack-livereload"
end

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM