坚持ROR教程CH 9 Ex。 9

时间:2012-10-24 22:35:40

标签: ruby-on-rails

我被困住了,无法弄清楚我哪里出错了。我找到了类似的主题但没有解决这些错误的方法:

1)用户页面删除链接作为管理员用户管理员访问索引页面

 Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
   expected link "delete" to return something
 # ./spec/requests/user_pages_spec.rb:127:in `block (5 levels) in <top (required)>'

2)用户页面删除链接作为管理员用户管理员访问索引页面应该能够删除另一个用户

 Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
 Capybara::ElementNotFound:
   no link with title, id or text 'delete' found
 # (eval):2:in `click_link'
 # ./spec/requests/user_pages_spec.rb:129:in `block (6 levels) in <top (required)>'
 # ./spec/requests/user_pages_spec.rb:129:in `block (5 levels) in <top (required)>'

测试代码

describe "delete links" do

  it { should_not have_link('delete') }
  describe "as an admin user" do
    let(:admin) { FactoryGirl.create(:admin) }
      before do
        sign_in admin
        visit users_path
      end

      it { should have_link('delete', href: user_path(User.first)) }
      it "should be able to delete another user" do
      expect { click_link('delete') }.to change(User, :count).by(-1)
    end
    it { should_not have_link('delete', href: user_path(admin)) }
  end
end

控制器:

 def
   User.find(params[:id]).destroy
    flash[:success] = "User destroyed."
    redirect_to users_url
  end

指数:

<% provide(:title, 'All users') %>
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <%= render @users %>
</ul>

<%= will_paginate %>

部分

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure?" } %>
  <% end %>
</li>

非常感谢任何指导/帮助......

2 个答案:

答案 0 :(得分:1)

也许一年后我来了,但我刚刚说过要从同一个教程(Rails tutorial from Michael Hartl)学习Ruby on Rails并面对同样的问题。

这里我发现了什么以及如何修复(感谢我们的伙伴们,我浏览了页面源代码并发现了问题。):

在教程中,Listing 9.43说:

  

要求'spec_helper'

     

描述“用户页面”

     

subject {page}

     

描述“index”做

let(:user) { FactoryGirl.create(:user) }

before do
  sign_in user
  visit users_path
end

it { should have_title('All users') }
it { should have_content('All users') }

describe "pagination" do
  .
  .
  .
end

describe "delete links" do

  it { should_not have_link('delete') }

  describe "as an admin user" do
    let(:admin) { FactoryGirl.create(:admin) }
    before do
      sign_in admin
      visit users_path
    end

    it { should have_link('delete', href: user_path(User.first)) }
    it "should be able to delete another user" do
      expect do
        click_link('delete', match: :first)
      end.to change(User, :count).by(-1)
    end
    it { should_not have_link('delete', href: user_path(admin)) }
  end
end   end   .   .   . end

检查与“删除链接”相关的部分如何在“分页”部分之后。 分页解释从Listing 9.33开始,并有下一个代码:

  

要求'spec_helper'

     

描述“用户页面”

     

subject {page}

     

描述“索引”做       let(:user){FactoryGirl.create(:user)}       之前(:每个)做         sign_in用户         访问users_path       端

it { should have_title('All users') }
it { should have_content('All users') }

describe "pagination" do

  before(:all) { 30.times { FactoryGirl.create(:user) } }
  after(:all)  { User.delete_all }

  it { should have_selector('div.pagination') }

  it "should list each user" do
    User.paginate(page: 1).each do |user|
      expect(page).to have_selector('li', text: user.name)
    end
  end
end   end   .   .   . end

我意识到after(:all) { User.delete_all }语句在该部分之后擦除了测试数据库中的所有记录(让我继续,因为这很明显:P)。当RSpec测试继续“删除链接”时,会创建任何用户。

下一步是创建一个新的'admin'用户,并且此时是测试数据库中唯一的用户。正如预期的那样:

  

它{should have_link('delete',href:user_path(User.first))}

将失败,因为当前用户无法删除自身,并且没有任何“删除”链接。以同样的方式,下一个测试也将失败:

  

click_link('删除',匹配::首先)

解决方案(解决方法或w / e:D):

我相信有一些方法可以解决这个问题,我认为有两种方法(第一种是我使用的那种)

一个。我添加了一个非管理员用户,在管理员用户尝试登录之前,这让我有2个用户进入测试数据库,如下面的代码:

  

描述“删除链接”       它{should_not have_link('删除')}

describe "as an admin user" do
    let(:admin) { FactoryGirl.create(:admin) }
    before do
        FactoryGirl.create(:user, name: "test", email: "test@example.com")
        sign_in admin
        visit users_path
    end

    it { should have_link('delete', href: user_path(User.first)) }
    it "should be able to delete another user" do
        expect do
            click_link('delete', match: :first)
        end.to change(User, :count).by(-1)
    end
    it { should_not have_link('delete', href: user_path(admin)) }
end   end

声明FactoryGirl.create(:user, name: "test", email: "test@example.com")完成了工作。

B中。移动“分页”部分之前的所有“删除链接”部分,因为我想在这一点上,至少存在在 * user_pages_spec.rb * 的开头创建的第一个用户。

就是这样,通过这个,我可以获得绿色“删除链接”的新功能。

我的两分钱。

iVieL。

答案 1 :(得分:0)

第一个固定测试是在那里忽略其余的。如果您的解决方案如下所示,那么您就是金色的。将整个块移动到分页内容之上的第二个想法也有效。

describe 'delete links' do
    it { should_not have_link('delete') }

    describe 'as an admin user' do
        let(:admin) { FactoryGirl.create(:admin) }
        before do
            FactoryGirl.create (:user) 
            sign_in admin
            visit users_path
        end

        it { should have_link('delete', href: user_path(User.first)) }
        it 'should be able to delete another user' do
          expect { click_link('delete') }.to change(User, :count).by(-1) 
        end
        it { should_not have_link('delete', href: user_path(admin)) }
    end
end