简体   繁体   English

如何在Rails的测试环境中添加Rack Middleware?

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

Situation 情况

I am currently testing a Rails app using Capybara . 我目前正在使用Capybara测试Rails应用。 In addition, I was using Guard with its extension guard-livereload to automatically reload my browser as soon as relevant source files changed. 此外,一旦相关源文件发生更改,我正在使用Guard及其扩展名guard-livereload来自动重新加载浏览器。

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. 由于save_and_open_page方法无法正确显示样式表,因此我将此解决方案应用于该问题,其中将临时视图转储capybara.html放置在/public/文件夹中,以确保资产的可访问性。

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. 现在,由于LiveReload就像开发中的魅力一样工作,我想在功能测试期间使用它来自动重新加载/public/capybara.html而不是/public/capybara.html打开它。

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. 由于某种原因,我只能将负责重新加载页面的Rack中间件插入到developerent-environment内的中间件堆栈中,而不能将其插入到test-environment中。 I use the following code for insertion: 我使用以下代码进行插入:

/config/environments/development.rb /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 /config/environments/test.rb使用相同的方法时,会发生以下错误

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. 由于我对Rails还是很陌生,所以我真的不知道从哪里开始。 As far as I know, trying to require the file manually wouldn't really be The Rails Way TM . 据我所知,尝试手动要求该文件实际上不是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 : 我相信您应该在Rack::LiveReload的测试环境中包括Rack::LiveReload 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. 在您的Gemfile中,您可能只在开发组中加载了rack-livereload gem-为此,您需要在开发和测试组中加载它。 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. 话虽如此,您确实希望测试环境尽可能地模仿生产,因此在测试环境中运行机架交付负载通常不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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