2012-03-27 88 views
0

我无法弄清楚为什么我的测试失败。我认为它与我使用的删除方法有关。无论出于何种原因,我无法获得适用于删除操作的链接。该网站的代码有效,但我似乎无法让Rspec映射到网站上发生的事情。有任何想法吗?什么是适当的Rspec语法

查看:

<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的测试:

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的输出:

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)>' 

而这里的运行的输出 “耙路线”:

 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))) 
+0

错误的其余部分是什么? (您只发布了rspec输出的前3行) – 2012-03-27 23:47:41

+0

好的,我用完整的错误消息更新了我的帖子。 – 2012-03-27 23:55:22

+0

美丽!那样做了。谢谢! – 2012-03-28 00:17:59

回答

1

不完全确定为什么会发生这种情况。 但日志指出的是,鉴于企图使指向一个链接到hunts#show

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

有像在视图中的第一个链接的例子:

<%= link_to hunt.name, hunt %> 

我假设的link_to没有得到狩猎的id并且用一个RoutingError逃脱。 hunt可能为零?

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

固定的:

无论哪种方式,奔说,与更换前行。