Rspec: how to check the Rails-url_for helper?

Using RSpec, how do I test the hell_for helper method to make sure it is going to the correct controller and action with a specific format?

+3


source to share


2 answers


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

      

+3


source


You can check that the named routes are being directed to the correct location. Routing specs live in spec/routing

, so inspec/routing/widget_routes_spec.rb

the following specifications will be added:
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 many other options for testing routes outlined in the routing specification specification .

+1


source







All Articles