使用RSpec,如何测试url_for辅助方法以确保它使用特定格式转到正确的控制器和操作?
I had to do the following --
Add this to 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
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.