在集成测试中使用命名路由是否合适? [英] Is it appropriate to use named routes in integration tests?

查看:34
本文介绍了在集成测试中使用命名路由是否合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中哪一种是编写集成(请求)测试的正确"方式.

Which one of these is the "right" way to write integration (requests) tests.

it "should be successful" do
  get "/about/terms"
  response.should be_success
end

it "should be successful" do
  get about_terms_path
  response.should be_success
end

推荐答案

about_terms_path 是正确的,因为自定义路由路径可能会在路由文件中更改,但路由名称应保持不变.

The about_terms_path is correct because the custom route path may change in the routes file but the name of the route should stay the same.

前者会导致脆弱的测试.

The former would lead to brittle tests.

如果路由的名称发生更改,则 Rails 应用中对该路由的所有引用都需要更改,您的 rspec 测试也是如此.

If the name of the route changed, all references to that route in the Rails app would need changing and so would your rspec tests.

如果路由的路径发生变化,则您的 Rails 应用或 rspec 测试中的任何内容都不需要更改.

If the path of the route changed, nothing in your Rails app or rspec tests would need changing.

如果您想测试路线,请查看 https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs

If you want to test the routes, check this out https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs

这篇关于在集成测试中使用命名路由是否合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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