引发路由未找到错误
我现在正在写一本关于 Rails 3 的书,过去的我在第 3 章左右写过,当运行特定功能时会生成路由错误。现在,我不像我那样去写不真实的东西,所以我很确定这种情况过去发生过一次。
我自己还无法复制该场景,但我非常有信心这是环境文件中被遗忘的设置之一。
要重复此问题:
- 生成一个新的 Rails 项目
- 重要:删除
public/index.html
文件 - 将 cucumber-rails 和 capybara 添加到
Gemfile
- run
bundle install
- run
rails g cucumber:sculpture
- 生成一个新功能,将其命名为
features/creating_projects.feature
- 在这里面功能放置:
这:
Feature: Creating projects
In order to value
As a role
I want feature
Scenario: title
Given I am on the homepage
当您使用 bundle exec cucumber features/creating_projects.feature
运行此功能时,它应该失败并出现“没有路由匹配/”错误,因为您没有' t 定义根路由。然而,我和其他人发现事实并非如此。
现在我已经在 test.rb 中设置了一个设置,它将显示此异常页面,但我宁愿 Rails 对异常进行硬引发,以便它在 Cucumber 中显示为失败一步,就像我很确定以前那样,而不是一个过渡步骤。
有谁知道自去年五月份以来 Rails 不这样做会发生什么变化?我非常有信心它是 config/environments/test.rb
中的某些设置,但我一生都无法弄清楚。
I'm writing a book on Rails 3 at the moment and past-me has written in Chapter 3 or so that when a specific feature is run that a routing error is generated. Now, it's unlike me to go writing things that aren't true, so I'm pretty sure this happened once in the past.
I haven't yet been able to duplicate the scenario myself, but I'm pretty confident it's one of the forgotten settings in the environment file.
To duplicate this issue:
- Generate a new rails project
- important: Remove the
public/index.html
file - Add cucumber-rails and capybara to the "test" group in your
Gemfile
- run
bundle install
- run
rails g cucumber:skeleton
- Generate a new feature, call it
features/creating_projects.feature
- Inside this feature put:
This:
Feature: Creating projects
In order to value
As a role
I want feature
Scenario: title
Given I am on the homepage
When you run this feature using bundle exec cucumber features/creating_projects.feature
it should fail with a "No route matches /" error, because you didn't define the root route. However, what I and others are seeing is that it doesn't.
Now I've set a setting in test.rb
that will get this exception page to show, but I would rather Rails did a hard-raise of the exception so that it showed up in Cucumber as a failing step, like I'm pretty sure it used to, rather than a passing step.
Does anybody know what could have changed since May-ish of last year for Rails to not do this? I'm pretty confident it's some setting in config/environments/test.rb
, but for the life of me I cannot figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我研究 Rails 源代码后,测试环境中似乎缺少负责引发异常
ActionController::RoutingError
的ActionDispatch::ShowExceptions
中间件。通过运行rake middleware
和rake middleware RAILS_ENV=test
进行确认。您可以在 https:// github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L152 它返回
X-Cascade =>; 'pass'
标头,ActionDispatch::ShowExceptions
负责拾取它(在 https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb# L52)所以您看到测试用例通过的原因是
rack-mount
返回“Not Found”文本,状态为 404。我会责怪人们并得到它会为你解决。这里的条件是: https://github。 com/rails/rails/blob/master/railties/lib/rails/application.rb#L159。如果设置为 true,则错误会被正确转换,但我们会得到错误页面输出。如果为 false,则根本不会加载此中间件。等等...
更新:要清除前一个区块,您将进入死胡同。如果您将
action_dispatch.show_exceptions
设置为false
,您将无法加载该中间件,从而导致rack-mount
出现 404 错误被渲染了。然而,如果您将action_dispatch.show_exceptions
设置为true
,该中间件将被加载,但它将挽救错误并为您呈现一个漂亮的“异常”页面。After I investigate the Rails source code, it seems like the
ActionDispatch::ShowExceptions
middleware that responsible of raising exceptionActionController::RoutingError
is missing in the test environment. Confirmed by runningrake middleware
andrake middleware RAILS_ENV=test
.You can see that in https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L152 it's returning
X-Cascade => 'pass'
header, and it'sActionDispatch::ShowExceptions
's responsibility to pick it up (in https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L52)So the reason you're seeing that your test case is passing because
rack-mount
is returning "Not Found" text, with status 404.I'll git blame people and get it fix for you. It's this conditional here: https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L159. If the setting is true, the error got translated right but we got error page output. If it's false, then this middleware doesn't get loaded at all. Hold on ...
Update: To clearify the previous block, you're hitting the dead end here. If you're setting
action_dispatch.show_exceptions
tofalse
, you'll not get that middleware loaded, resulted in the 404 error fromrack-mount
got rendered. Whereas if you're settingaction_dispatch.show_exceptions
totrue
, that middleware will got loaded but it will rescue the error and render a nice "exception" page for you.