1

嗨,我是通过 Hartl 工作的新手。我被困在第 10 章的微博上。当我跑步时

bundle exec rspec spec/requests/authentication_pages_spec.rb

终端返回这些失败消息:

Failures:

1) authentication authorization in the Users controller submitting to the update action 
 Failure/Error: before { put user_path(user) }
 AbstractController::ActionNotFound:
   The action 'update' could not be found for UsersController
 # ./spec/requests/authentication_pages_spec.rb:78:in `block (5 levels) in <top (required)>'

2) authentication authorization in the Users controller as wrong user submitting a PUT request to the Users#update action 
 Failure/Error: before { put user_path(wrong_user) }
 AbstractController::ActionNotFound:
   The action 'update' could not be found for UsersController
 # ./spec/requests/authentication_pages_spec.rb:110:in `block (6 levels) in <top (required)>'

3) authentication authorization in the Users controller 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:122:in `block (6 levels) in <top (required)>'

Finished in 1.54 seconds
21 examples, 3 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:79 # authentication authorization in the Users controller submitting to the update action 
rspec ./spec/requests/authentication_pages_spec.rb:111 # authentication authorization in the Users controller as wrong user submitting a PUT request to the Users#update action 
rspec ./spec/requests/authentication_pages_spec.rb:123 # authentication authorization in the Users controller as non-admin user submitting a DELETE request to the Users#destroy action 

这是我的 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
  let(:user) { FactoryGirl.create(:user) }

  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

 describe "in the Microposts controller" do

    describe "submitting to the create action" do
      before { post microposts_path }
      specify { response.should redirect_to(signin_path) }
    end

    describe "submitting to the destroy action" do
      before { delete micropost_path(FactoryGirl.create(:micropost)) }
      specify { response.should redirect_to(signin_path) }
    end
  end

 describe "as wrong user" do
  let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
  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
end

我不确定是什么原因造成的。有任何想法吗?谢谢西蒙

4

1 回答 1

1

您是否在 user_controller.rb 中定义了更新和销毁操作?

这是我的供参考。

class UsersController < ApplicationController
  before_filter :signed_in_user,  only: [:index, :edit, :update]
  before_filter :correct_user,    only: [:edit, :update]
  before_filter :admin_user,      only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(params[:user])
      sign_in @user
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

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

  private
    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_path, notice: "Please sign in"
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to root_path unless current_user?(@user)
    end

    def admin_user
      redirect_to root_path unless current_user.admin?
    end
end
于 2012-06-22T20:15:18.537 回答