Ben Downey
Ben Downey

Reputation: 2665

What is the appropriate Rspec syntax

I can't figure out why my test is failing. I think it has to do with the delete method I'm using. For whatever reason, I can't get the link right for the delete action. The code for the site works, but I can't seem to have Rspec to map onto what's happening with the site. Any ideas?

View:

    <li>
      <%= link_to hunt.name, hunt %>
      <% if current_user && current_user.admin? %>
        | <%= link_to "edit", edit_hunt_path(hunt) %>
        | <%= link_to "delete", hunt, :method => :delete, :confirm => "You sure?",
                                    :title => "Delete #{hunt.name}" %>    
      <% end %>
    </li>

Rspec test:

require 'spec_helper'

describe HuntsController do
  render_views 

  describe "GET 'index'" do
  ...
    describe "for users who are admins" do
       before(:each) do
          admin = FactoryGirl.create(:user, :email => "[email protected]", :admin => true)
          test_sign_in(admin)
        end
        ...
        it "should show delete link" do
          get 'index'
          Hunt.paginate(:page => 1).each do |hunt|
            response.should have_selector('a', :href => hunt_path(@hunt)   , :content => 'delete')
          end
        end              
    end  
   end

Rspec output:

   1) HuntsController GET 'index' for users who are admins should show delete link
     Failure/Error: response.should have_selector('a', :href => hunt_path(@hunt)   , :content => 'delete')
     ActionController::RoutingError:
       No route matches {:action=>"show", :controller=>"hunts"}
     # ./spec/controllers/hunts_controller_spec.rb:64:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/hunts_controller_spec.rb:63:in `block (4 levels) in <top (required)>' 

And here's the output of running "rake routes":

        users GET    /users(.:format)                       {:action=>"index", :controller=>"users"}
              POST   /users(.:format)                       {:action=>"create", :controller=>"users"}
     new_user GET    /users/new(.:format)                   {:action=>"new", :controller=>"users"}
    edit_user GET    /users/:id/edit(.:format)              {:action=>"edit", :controller=>"users"}
         user GET    /users/:id(.:format)                   {:action=>"show", :controller=>"users"}
              PUT    /users/:id(.:format)                   {:action=>"update", :controller=>"users"}
              DELETE /users/:id(.:format)                   {:action=>"destroy", :controller=>"users"}
        hunts GET    /hunts(.:format)                       {:action=>"index", :controller=>"hunts"}
              POST   /hunts(.:format)                       {:action=>"create", :controller=>"hunts"}
     new_hunt GET    /hunts/new(.:format)                   {:action=>"new", :controller=>"hunts"}
    edit_hunt GET    /hunts/:id/edit(.:format)              {:action=>"edit", :controller=>"hunts"}
         hunt GET    /hunts/:id(.:format)                   {:action=>"show", :controller=>"hunts"}
              PUT    /hunts/:id(.:format)                   {:action=>"update", :controller=>"hunts"}
              DELETE /hunts/:id(.:format)                   {:action=>"destroy", :controller=>"hunts"}
     sessions POST   /sessions(.:format)                    {:action=>"create", :controller=>"sessions"}
  new_session GET    /sessions/new(.:format)                {:action=>"new", :controller=>"sessions"}
      session DELETE /sessions/:id(.:format)                {:action=>"destroy", :controller=>"sessions"}
                     /hunts(.:format)                       {:controller=>"hunts", :action=>"index"}
       signup        /signup(.:format)                      {:controller=>"users", :action=>"new"}
       signin        /signin(.:format)                      {:controller=>"sessions", :action=>"new"}
      signout        /signout(.:format)                     {:controller=>"sessions", :action=>"destroy"}
      contact        /contact(.:format)                     {:controller=>"pages", :action=>"contact"}
        about        /about(.:format)                       {:controller=>"pages", :action=>"about"}
         help        /help(.:format)                        {:controller=>"pages", :action=>"help"}
         root        /                                      {:controller=>"pages", :action=>"home"}
                     /:controller(/:action(/:id(.:format))) 

Upvotes: 0

Views: 208

Answers (1)

martinjlowm
martinjlowm

Reputation: 871

Not exactly sure why this happens. But the log states that the view attempts to render a link which points to hunts#show

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

has examples like the first link in the view:

<%= link_to hunt.name, hunt %>

I assume that the link_to fails to get hunt's id and escapes with an RoutingError. hunt could be nil?

Either way, Ben says replacing the previous line with:

<%= link_to hunt.name, hunt_path(hunt) %>

fixed this.

Upvotes: 1

Related Questions