如何在Rails中使用Rspec(或如何正确使用它)

时间:2012-03-27 11:28:15

标签: ruby-on-rails rspec factory-bot

我似乎无法通过这些Rspec测试。我只是不知道我到底做错了什么。有什么建议?当管理员登录时,这些测试确保每个搜索都有一个编辑和删除链接。我正在使用FactoryGirl生成测试管理员用户和测试搜索。

以下是测试。

    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 edit link" do
      get 'index'
      Hunt.each do |hunt|
        response.should have_selector('a', :href => "edit",
                                       :content => "edit")    
      end
    end

    it "should show delete link" do
      get 'index'
      Hunt.each do |hunt|
        response.should have_selector('a', :href => "delete"  ,
                                       :content => "delete")  
      end
    end

这是我正在测试的代码。它来自views / hunts.html.erb

    <h1>All Hunts</h1>

    <%= will_paginate %>

    <ul>
      <% @hunts.each do |hunt| %>
            <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>
      <% end %>
    </ul>
    <%= will_paginate %>

    <%= link_to( "Create New Hunt", '/hunts/new') %>  

这是狩猎控制器的相关部分。

class HuntsController < ApplicationController

  def index
     @title = "All Hunts"
     @hunts = Hunt.paginate(:page => params[:page])
  end

    ....
end

当我运行rspec时,就会出现这种情况。

    </li>
            <li>
      <a href="/hunts/30">---
   - optio
   - et
   - sunt
   </a>
        | <a href="/hunts/30/edit">edit</a>
        | <a href="/hunts/30" data-confirm="You sure?" data-method="delete" rel="nofollow" title="Delete ---
   - optio
   - et
   - sunt
   ">delete</a>

最后,这是我的factories.rb文件。

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}   
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end

  factory :hunt do
    name "Test Hunt" 
  end
end         

更新:所以我从原始测试中删除了“a”。我还重写了测试,以便它在第1页上查看每次搜索。

      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 edit link" do
          get 'index'
          Hunt.paginate(:page => 1).each do |hunt|
            response.should have_selector(:href => "edit",
                                           :content => "edit")
          end 
        end

        it "should show delete link" do
          get 'index'
          Hunt.paginate(:page => 1).each do |hunt|
            response.should have_selector(:href => "delete",
                                           :content => "delete")  
          end
        end
      end 

所以现在我有一个不同的错误:

HuntsController GET 'index' for users who are admins should show edit link
 Failure/Error: response.should have_selector(:href => "edit",
 Nokogiri::CSS::SyntaxError:
   unexpected '{' after ''

我试过谷歌搜索这个错误,但没有太多运气。之前看过吗?我不确定我弄乱了什么语法。

1 个答案:

答案 0 :(得分:1)

如果将选择器更改为以下内容该怎么办:

it "should show an 'edit' link" do
  get :index
  ...
  response.should have_selector('a', :href => edit_hunt_path(hunt), :content => 'edit')
  ...
end

如果您使用的是Capybara,那么您只需使用have_link()而不是have_selector()。