2012-10-24 49 views
1

我被卡住了,無法弄清楚我錯了。我也發現了類似的話題,但沒有解決這些錯誤:卡在ROR教程CH 9 Ex。 9

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> 

任何指導/幫助將不勝感激....

+0

叫什麼名字和局部的路徑?在'visit users_path'後面顯示'puts page.body'是什麼? – prusswan

+0

你可以添加一個'save_and_open_page'來確認,但我敢打賭你沒有任何'@用戶'來渲染;所以沒有刪除鏈接顯示。確認? –

+0

prusswan,感謝您的反饋,部分是_user.html.erb並位於views/users文件夾中。我不確定你在哪裏看到放置page.body – sethb78

回答

1

也許我就在一年後,但我剛剛說過要從同一教程(Rails tutorial from Michael Hartl)學習Ruby on Rails,並面臨同樣的問題。

在這裏我找到了什麼,我怎麼可以做修復(感謝我們的隊友commets,我通過網頁源代碼去,發現這個問題。):

從教程,Listing 9.43說:

需要 'spec_helper'

形容 「用戶頁面」 做

主題{PAGE}

形容「指數」做

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'

形容 「用戶頁面」 做

主題{PAGE}

形容 「指數」做 讓((用戶){FactoryGirl。創建(:用戶)} 前(:每個)做 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測試繼續「刪除鏈接」時,會創建任何用戶。

下一步是創建一個新的'管理員'用戶,並且此時是測試數據庫中唯一的用戶。正如預期:

它{應當have_link( '刪除',HREF:user_path(User.first))}

會失敗,因爲當前用戶不能刪除本身,它沒有任何'刪除'鏈接。以相同的方式對下一個測試將被太失敗:

click_link( '刪除',匹配::第一)

溶液(變通方法或W/E:d):

我相信有一些方法可以解決這個問題,我認爲有兩個(我使用的是第一個)

A.我添加一個非管理員用戶,在管理員用戶嘗試登錄之前,我有2個用戶加入測試數據庫,如下面的代碼:

形容 「刪除鏈接」 別 它{should_not have_link( '刪除')}

describe "as an admin user" do 
    let(:admin) { FactoryGirl.create(:admin) } 
    before do 
     FactoryGirl.create(:user, name: "test", email: "[email protected]") 
     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[email protected]")所做的工作。

B.將所有「刪除鏈接」部分的「分頁」節之前,因爲我在這一點猜測,存在至少在開始* user_pages_spec.rb *創建的第一個用戶。

就是這樣,藉此,我可以變綠的「刪除鏈接」新功能。

我的兩毛錢。

iVieL。

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