[英]Rspec: how to test Rails' url_for helper?
使用RSpec,如何测试url_for辅助方法以确保它使用特定格式转到正确的控制器和操作?
I had to do the following -- 我必须做以下事情 -
Add this to spec_helper.rb: 将其添加到spec_helper.rb:
Rspec.configure do |config|
config.include Rails.application.routes.url_helpers # url_for
end
Then: 然后:
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
You can test that named routes are routed to the correct place. 您可以测试命名路由是否路由到正确的位置。 Routing specs live in spec/routing
,so the following specs would be added to spec/routing/widget_routes_spec.rb
路由规范存在于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
There are a lot of other options for testing routes described in the routing specs documentation . 路由规范文档中描述的测试路径还有很多其他选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.