1

I want to test the permalink action in my articles controller, which is using a named route (/permalink/terms-of-use):

map.permalink 'permalink/:permalink', 
              :controller => :articles, :action => :permalink, :as => :permalink

This is the spec:

describe "GET permalink" do
  it "should visit an article" do
    get "/permalink/@article.permalink"
  end
end

But I get this error:

ActionController::RoutingError in 'ArticlesController permalink renders the page' No route matches {:controller=>"articles", :action=>"/permalink/@article.permalink"}

UPDATE:

any idea how to write the GET?

2 Answers 2

4

The error is because you're passing an entire URL to a method that expects a name of one of the controller's action methods. If I understand correctly, you're trying to test several things at once.

Testing that a route has a name is different from testing a route is different from testing a controller action. Here's how I test a controller action (this probably comes as no surprise). Note that I'm matching your naming, not recommending what I'd use.

In spec/controllers/articles_controller_spec.rb,

describe ArticlesController do
  describe '#permalink' do
    it "renders the page" do
      # The action and its parameter are both named permalink
      get :permalink :permalink => 666
      response.should be_success
      # etc.
    end
  end
end

Here's how I test a named route with only rspec-rails:

In spec/routing/articles_routing_spec.rb,

describe ArticlesController do
  describe 'permalink' do

    it 'has a named route' do
      articles_permalink(666).should == '/permalink/666'
    end

    it 'is routed to' do
      { :get => '/permalink/666' }.should route_to(
        :controller => 'articles', :action => 'permalink', :id => '666')
    end

  end
end

Shoulda's route matcher is more succinct while still providing a nice description and failure message:

describe ArticlesController do
  describe 'permalink' do

    it 'has a named route' do
      articles_permalink(666).should == '/permalink/666'
    end

    it { should route(:get, '/permalink/666').to(
      :controller => 'articles', :action => 'permalink', :id => '666' })

  end
end

AFAIK neither RSpec nor Shoulda have a specific, concise way of testing named routes, but you could write your own matcher.

7
  • with our first example I get a routing error, i need to test 'permalink/a_name'. Sorry my question was unclear. I need to test the permalink action, but I am using a named route. I need to know the right syntax for get ...
    – rtacconi
    Mar 30, 2011 at 13:49
  • When you test the action, don't worry about its route. Just test the action directly as in the first code example above. Testing the route is a different issue; see the other two examples for that. Mar 30, 2011 at 18:04
  • your first example does not work because a get is created having articles in the url, i.e. /articles/permalink/name_of_article but I need /permalink/name_of_the_article and RSpec does not allow me to do that.
    – rtacconi
    Mar 30, 2011 at 19:57
  • 1
    I adjusted my examples to match what I think you're trying to do. The important point is not the specifics of the route, though, but the difference between testing an action and testing a route. In an RSpec controller test, get's first parameter is the name of an action, not a path. Provide the rest of the information in the path using parameters. Mar 31, 2011 at 1:06
  • Your first example is very similar to my test, but it gives: ActionController::RoutingError in 'ArticlesController GET permalink renders the page' No route matches {:controller=>"articles", :permalink=>"link", :action=>"permalink"}
    – rtacconi
    Apr 8, 2011 at 21:26
0
describe "GET permalink" do
  it "should visit an article" do
    get "/permalink/#{@article.permalink}"
  end
end
1
  • 4
    No. I get this: ActionController::RoutingError in 'ArticlesController GET permalink should visit an article' No route matches {:controller=>"articles", :action=>"/permalink/link"}
    – rtacconi
    Nov 18, 2011 at 10:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.