什么是适当的Rspec语法

时间:2012-03-27 23:40:07

标签: ruby-on-rails 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 => "admin@example.com", :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)>' 

这是运行&#34; rake路线的输出&#34;:

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

1 个答案:

答案 0 :(得分:1)

不确定为什么会发生这种情况。 但是日志指出视图尝试呈现指向hunts#show

的链接

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

有示例,例如视图中的第一个链接:

<%= link_to hunt.name, hunt %>

我假设link_to无法获取hunt的id并且因为RoutingError而逃脱。 hunt可能是零吗?

无论哪种方式,Ben说用以下代替前一行:

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

解决了这个问题。