仍然建议使用Minitest在Rails 4中测试路由吗? [英] Is it still advisable to test routes in Rails 4 with Minitest?

查看:91
本文介绍了仍然建议使用Minitest在Rails 4中测试路由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 3中,在MiniTest中编写功能测试时,我习惯于分别测试路由和测试控制器动作.我从测试指南-第9节:测试路线中得到了这个想法.但是,将我的应用程序升级到Rails 4之后,我注意到,如果我没有为get|patch|post|delete方法提供适当的参数集,那么控制器动作测试本身就开始对未知路由了如指掌.

In Rails 3, when writing functional tests in MiniTest, I got in the habit of testing routes separately from testing my controller actions. I got the idea from the Rails Guide on Testing - Section 9: Testing Routes. However, after upgrading my application to Rails 4 I noticed that the controller action tests themselves have started balking about unknown routes if I don't supply the get|patch|post|delete method with a proper set of params.

例如,给定的路线:

# config/routes.rb
namespace "api" do
  namespace "v2", defaults: { format: :json } do
    resources :users do
      resources :posts do
        resources :comments
      end
    end
  end
end

和功能测试:

# test/controllers/api/v2/comments_controller_test.rb
describe Api::V2::CommentsController
  it "does something" do
    get :index
  end
end

在Rails 3中,上述方法将起作用.但是在Rails 4中,我收到一个URL生成错误:

In Rails 3, the above would work. But in Rails 4 I get a URL generation error:

ActionController :: UrlGenerationError:没有路由匹配{:action =>"index",:controller =>"api/v2/comments"}

ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"api/v2/comments"}

据此,我可以推断出get帮助器在尝试定位控制器和操作时,根本无法匹配路由文件中的路由.很公平.我可以通过更改get调用来解决此问题,以包括满足嵌套路由所需的参数,如下所示:

From this I can infer that the get helper simply failed to match a route from the routes file when trying to locate the controller and action. Fair enough. I can fix this by changing the get call to include the parameters needed to satisfy the nested route, like this:

# test/controllers/api/v2/comments_controller_test.rb
describe Api::V2::CommentsController
  it "does something" do
    get :index, { user_id: "1", post_id: "1" }
  end
end

...然后一切恢复正常.

... then all is well again.

所以我的问题是,由于在Rails 3中不是这种情况,现在可以信任控制器动作测试以在Rails 4+中完全测试/验证我的路由了吗?还是在测试路线上还有其他优势?也许路由测试覆盖了控制器动作测试未覆盖的其他角度? (注意:我不是要对哪种测试方法提出意见,而是要就路由需求在路由集成测试和控制器动作测试之间寻求功能上的区别.)

So my question is, since it wasn't the case in in Rails 3, is it now OK to trust the controller action tests to fully test/validate my routes in Rails 4+? Or is there additional advantage in testing the routes as well still? Is there, perhaps, some other angle that the route tests cover that the controller action tests don't cover? (Note: I'm not asking for an opinion on what's good to test; I'm asking for functional differences between route integration tests and controller action tests in regard to route requirements.)

此外,我在Rails 4发行说明(或Minitest)中找不到这种行为改变的具体参考,所以我想知道为什么首先进行这种行为改变.我认为这不是一件坏事-我认为这很不错-但我感到很奇怪,没有在某处的变更日志中看到它.而且我认为get|patch|post|delete方法的一半目的是使您不必首先考虑路由所需的参数.

Also, I couldn't find a specific reference to this change in behavior in the Rails 4 release notes (or in Minitest), so I'm wondering why this behavior change was made in the first place. I don't think it's a bad thing -- I think it's good -- but I feel weird not seeing it mentioned in a changelog somewhere. And I thought that half the point of the get|patch|post|delete methods were to free you from having to think about what params are needed for routing in the first place.

为完整起见,这是我将为此使用的路由测试:

For completeness, here is the route test I would use for this:

describe "CommentsController Route Integration Test" do
  let(:default_options) {
    { controller: "api/v2/comments",
      user_id: "1",
      posts_id: "1",
      format: :json }
  }

  it "#index" do
    assert_routing "/api/v2/users/1/posts/1/comments",
                   default_options.merge(action: "index")
  end
end


更新

我一直在寻找ActionDispatch代码以寻找答案...到目前为止,我唯一能看到的是url_for的东西自Rails 3以来已经发生了很大变化,而ActionController :: UrlGenerationError类本身已经在Rails 4中添加了.因此,这些新的,更严格的路由要求可能是对ActionView和ActionController的去耦的偶然更改.


UPDATE

I've been looking through ActionDispatch code for an answer... the only thing I can see so far is that the url_for stuff has changed a lot since Rails 3 and that the ActionController::UrlGenerationError class itself has been added in Rails 4. So it could be that these new, more strict routing requirements are incidental change to the decoupling of ActionView and ActionController.

推荐答案

最近为一些新的控制器编写了功能测试,但没有编写任何路由集成测试,我非常有信心测试路由的额外工作是多余的.给出的就是说,是否有涵盖所有路线的功能测试.

After writing functional tests for a few new controllers recently, without writing any Route Integration Tests, I'm fairly confident that the additional work of testing routes is overkill. Given, that is, if there are functional tests covering all of the routes.

我的推理基本上是...在任何情况下,如果我向控制器动作的get|patch|post|delete调用提供了不正确的参数,它总是会失败.每当我为存在但缺少相应路由的控制器动作编写测试时,它总是失败.这样看来,较高级别的功能测试也可以很好地进行较低级别的路由集成测试-那么,为什么还要花很多精力呢?

My reasoning is basically that... in any case where I've supplied improper parameters to a get|patch|post|delete call into a controller action, it's always failed. And whenever I write a test for a controller action that exists but that is lacking a corresponding route it always fails. So it appears that the higher-level functional tests also exercise the lower level route integration tests just fine -- so why bother duplicating effort!

我想我将来仍会在此寻找更新的官方"字眼,但是现在,我决定不再担心只要存在重叠的功能测试就直接测试我的路线.

I guess I'll still be looking for an updated "official" word on this in the future, but for now I've decided to stop worrying about directly testing my routes whenever overlapping functional tests exist.

我进一步决定测试API路由可能仍然有用.这有助于确保与API一起发布的实际URL string 正确无误.请注意,测试字符串而不是命名路由很重要,因为命名路由会随着路由文件中资源的更改而改变.

I've further decided that testing API routes may still be useful. This helps to ensure that the actual URL string that is published with the API is actually correct and working. Note that it's important to test the string and not the named route as the named route can change with changes to the resources in the routes file.

这篇关于仍然建议使用Minitest在Rails 4中测试路由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆