Rspec:如何测试Rails的url_for助手?

kon*_*yak 4 rspec ruby-on-rails

使用RSpec,如何测试url_for辅助方法,以确保它以特定格式进入正确的控制器和动作?

kon*_*yak 5

我必须执行以下操作-

将此添加到spec_helper.rb:

Rspec.configure do |config|
  config.include Rails.application.routes.url_helpers  # url_for
end
Run Code Online (Sandbox Code Playgroud)

然后:

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
Run Code Online (Sandbox Code Playgroud)