1

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

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>

任何指导/帮助将不胜感激......

4

2 回答 2

1

也许一年后我来了,但我只是说要从同一个教程(Michael Hartl 的 Rails 教程)中学习 Ruby on Rails,并面临同样的问题。

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

在教程中,清单 9.43说:

需要'spec_helper'

描述“用户页面”做

主题{页面}

描述“索引”做

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

检查与“删除链接”相关的部分如何位于“分页”部分之后。分页说明从代码清单 9.33开始,代码如下:

需要'spec_helper'

描述“用户页面”做

主题{页面}

describe "index" do let(:user) { FactoryGirl.create(:user) } before(:each) do sign_in user visit users_path end

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' 用户,并且是此时测试数据库中唯一的一个。正如预期的那样:

它{应该有_link('删除',href:user_path(User.first))}

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

click_link('删除', 匹配: :first)

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

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

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

描述“删除链接”这样做 { should_not have_link('delete') }

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.

于 2013-09-06T21:50:51.967 回答
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         

结尾

于 2014-03-04T21:28:04.340 回答