使用RSpec,我如何测试url_for辅助方法以确保它使用特定格式转到正确的控制器和操作?
答案 0 :(得分:3)
我必须做以下事情 -
将此添加到spec_helper.rb:
Rspec.configure do |config|
config.include Rails.application.routes.url_helpers # url_for
end
然后:
describe "Article routing" do
it "routes to #index.atom" do
get('/articles.atom').
should route_to('articles#index', format: 'atom')
url_for(:controller => 'articles', :format => :atom, :only_path => true).
should == '/articles/index.atom'
end
end
答案 1 :(得分:1)
您可以测试命名路由是否路由到正确的位置。路由规范位于spec/routing
,因此以下规范将添加到spec/routing/widget_routes_spec.rb
describe "routes to the widgets controller" do
it "routes a named route" do
expect(:get => new_widget_path).
to route_to(:controller => "widgets", :action => "new")
end
end
有许多其他选项可用于测试routing specs documentation中描述的路线。