2012-06-22 175 views
0

嗨,我是Rails的新手,我已經設法在Hartl的第9章結束。我有3次失敗的測試來完成本章。Ruby on Rails教程Michael Hartl。第9章失敗的測試

終端返回此:

sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/ 
..........................................F...................................FF. 

Failures: 

1) authentication authorization as non-admin user submitting a DELETE request to the Users#destroy action 
Failure/Error: before { delete user_path(user) } 
AbstractController::ActionNotFound: 
    The action 'destroy' could not be found for UsersController 
# ./spec/requests/authentication_pages_spec.rb:110:in `block (5 levels) in <top (required)>' 

2) delete links as an admin user 
Failure/Error: it { should have_link('delete', href: user_path(User.first)) } 
    expected link "delete" to return something 
# ./spec/requests/user_pages_spec.rb:145:in `block (3 levels) in <top (required)>' 

3) delete links as an admin user should be able to delete another user 
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:147:in `block (4 levels) in <top (required)>' 
# ./spec/requests/user_pages_spec.rb:147:in `block (3 levels) in <top (required)>' 

Finished in 2.87 seconds 
81 examples, 3 failures 

Failed examples: 

rspec ./spec/requests/authentication_pages_spec.rb:111 # authentication authorization as non-admin user submitting a DELETE request to the Users#destroy action 
rspec ./spec/requests/user_pages_spec.rb:145 # delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:146 # delete links as an admin user should be able to delete another user 

這裏是我的authentication_pages_spec.rb

require 'spec_helper' 

describe "authentication" do 
subject { page } 
let(:user) { FactoryGirl.create(:user) } 

describe "signin page" do 
before { visit signin_path } 

it { should have_selector('h1', text: 'Sign in') } 
it { should have_selector('title', text: 'Sign in') } 
end 

describe "signin" do 
before { visit signin_path } 

describe "with invalid information" do 
    before { click_button "Sign in" } 

    it { should have_selector('title', text: 'Sign in') } 
    it { should have_selector('div.alert.alert-error', text: 'Invalid') } 

    describe "after visiting another page" do 
    before { click_link "Home" } 
    it { should_not have_selector('div.alert.alert-error') } 
    end 
end 

describe "with valid information" do 
    before { sign_in user } 

    it { should have_selector('title', text: user.name) } 

    it { should have_link('Users', href: users_path) } 
    it { should have_link('Profile', href: user_path(user)) } 
    it { should have_link('Settings', href: edit_user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 

    it { should_not have_link('Sign in', href: signin_path) } 

    describe "followed by signout" do 
    before { click_link "Sign out" } 
    it { should have_link('Sign in') } 
    end 
    end 
end 

describe "authorization" do 

describe "for non-signed-in users" do 

    describe "when attempting to visit a protected page" do 
    before do 
     visit edit_user_path(user) 
     fill_in "Email", with: user.email 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
    end 

    describe "after signing in" do 

     it "should render the desired protected page" do 
     page.should have_selector('title', text: 'Edit user') 
     end 
    end 
    end 
end 

    describe "in the Users controller" do 

    describe "visiting the edit page" do 
     before { visit edit_user_path(user) } 
     it { should have_selector('title', text: 'Sign in') } 
    end 

    describe "submitting to the update action" do 
     before { put user_path(user) } 
     specify { response.should redirect_to(signin_path) } 
    end 

    describe "visiting the user index" do 
     before { visit users_path } 
     it { should have_selector('title', text: 'Sign in') } 
    end 

end 

describe "as wrong user" do 
    let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") } 
    before { sign_in user } 

    describe "visiting Users#edit page" do 
    before { visit edit_user_path(wrong_user) } 
    it { should_not have_selector('title', text: full_title('Edit user')) } 
    end 

    describe "submitting a PUT request to the Users#update action" do 
    before { put user_path(wrong_user) } 
    specify { response.should redirect_to(root_path) } 
    end 
end 

describe "as non-admin user" do 
    let(:user) { FactoryGirl.create(:user) } 
    let(:non_admin) { FactoryGirl.create(:user) } 

    before { sign_in non_admin } 

    describe "submitting a DELETE request to the Users#destroy action" do 
    before { delete user_path(user) } 
    specify { response.should redirect_to(root_path) }   
    end 
end 
end 
end 

而且我user_pages_spec.rb

require 'spec_helper' 

describe "User pages" do 

subject { page } 

describe "index" do 

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

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

before do 
    sign_in user 
    visit users_path 
end 

it { should have_selector('title', text: 'All users') } 
it { should have_selector('h1', text: 'All users') } 

describe "pagination" do 

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

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

describe "signup page" do 
before { visit signup_path } 

it { should have_selector('h1', text: 'Sign up') } 
it { should have_selector('title', text: full_title('Sign up')) } 
end 

describe "profile page" do 
let(:user) { FactoryGirl.create(:user) } 

before { visit user_path(user) } 

it { should have_selector('h1', text: user.name) } 
it { should have_selector('title', text: user.name) }  
end 

describe "signup" do 

before { visit signup_path } 

let(:submit) { "Create my account" } 

describe "with invalid information" do 
    it "should not create a user" do 
    expect { click_button submit }.not_to change(User, :count) 
    end 

    describe "after submission" do 
    before { click_button submit } 

    it { should have_selector('title', text: 'Sign up') } 
    it { should have_content('error') } 
    end 
end 

describe "with valid information" do 

    before do 
    fill_in "Name",   with: "Example User" 
    fill_in "Email",  with: "[email protected]" 
    fill_in "Password",  with: "foobar" 
    fill_in "Confirmation", with: "foobar"   
    end 

    it "should create a user" do 
    expect { click_button submit }.to change(User, :count).by(1) 
    end 

    describe "after saving a user" do 
    before { click_button submit } 

    let(:user) { User.find_by_email("[email protected]") } 

    it { should have_selector('title', text: user.name) } 
    it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
    it { should have_link('Sign out') } 
    end 
end 
end 

describe "edit" do 
let(:user) { FactoryGirl.create(:user) } 
before do 
    sign_in user 
    visit edit_user_path(user) 
end 

describe "page" do 
    it { should have_selector('h1', text: "Update your profile") } 
    it { should have_selector('title', text: "Edit user") } 
    it { should have_link('change', href: 'http://gravatar.com/emails') } 
end 

describe "with invalid information" do 
    before { click_button "Save changes" } 

    it { should have_content('error') } 
    it { should_not have_content('Password digest') } 
end 

describe "with valid information" do 
    let(:new_name) { "New Name" } 
    let(:new_email) { "[email protected]" } 
    before do 
    fill_in "Name",    with: new_name 
    fill_in "Email",   with: new_email 
    fill_in "Password",   with: user.password 
    fill_in "Confirm Password", with: user.password 
    click_button "Save changes" 
    end 

    it { should have_selector('title', text: new_name) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should have_selector('div.alert.alert-success') } 
    specify { user.reload.name.should == new_name } 
    specify { user.reload.email.should == new_email } 
end 
end 
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 { click_link('delete') }.to change(User, :count).by(-1) 
    end 
    it { should_not have_link('delete', href: user_path(admin)) } 
    end 
end 

我真的不知道什麼在這裏走錯了,因爲我真的很想忠於教程。有任何想法嗎?謝謝西蒙。

+0

用戶的'索引'視圖如何?你能否粘貼它? –

回答

1

因爲您收到以下消息:

AbstractController::ActionNotFound: 
    The action 'destroy' could not be found for UsersController 

您的UsersController創建destroy行動。

class UsersController < ApplicationController 

    . 
    . 
    . 

    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 
    flash[:success] = 'User was deleted with success.' 
    redirect_to users_path 
    end 
end 

並創建鏈接以便能夠刪除每個用戶。

在你的`應用程序/視圖/用戶/ index.html.erb」,你應該有這樣的事情:

<% @users.each do |user| %> 
<p><%= link_to user.name, user_path(user), method: delete, confirm: 'Are you sure?' %></p> 
<% end %> 

希望它可以幫助你。

+0

問題依然存在。一模一樣。 –

+0

管理刪除第一次失敗 - 用戶控制器之一。第二次和第三次失敗仍然存在。 –

+0

確定已解決。謝謝 –

相關問題