引发路由未找到错误

发布于 2024-10-17 21:35:08 字数 1054 浏览 1 评论 0原文

我现在正在写一本关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-10-24 21:35:09

在我研究 Rails 源代码后,测试环境中似乎缺少负责引发异常 ActionController::RoutingErrorActionDispatch::ShowExceptions 中间件。通过运行 rake middlewarerake 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 exception ActionController::RoutingError is missing in the test environment. Confirmed by running rake middleware and rake 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's ActionDispatch::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 to false, you'll not get that middleware loaded, resulted in the 404 error from rack-mount got rendered. Whereas if you're setting action_dispatch.show_exceptions to true, that middleware will got loaded but it will rescue the error and render a nice "exception" page for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文