Ruby on Rails教程(mhartl)“第10章:销毁用户”autotest rspec删除'破坏'失败

时间:2011-10-21 17:14:04

标签: ruby-on-rails-3 rspec-rails railstutorial.org

我正在进行迈克尔·哈特尔的Rails Tutorial截屏视频+第10章的在线书籍,我被困在破坏用户的最后一章子。

控制台输出:

Failures:

      1) UsersController DELETE 'destroy' as an admin user should destroy the user

         Failure/Error: delete :destroy, :id => @user
         NameError:
           undefined local variable or method `users' for #<UsersController:0x00000104a2f698>
          ./app/controllers/users_controller.rb:47:in `destroy'
          ./spec/controllers/users_controller_spec.rb:321:in `block (5 levels) in <top (required)>'
          ./spec/controllers/users_controller_spec.rb:320:in `block (4 levels) in <top (required)>'

      2) UsersController DELETE 'destroy' as an admin user should redirect to the users page

         Failure/Error: delete :destroy, :id => @user
         NameError:
           undefined local variable or method `users' for #<UsersController:0x000001049bf370>
         ./app/controllers/users_controller.rb:47:in `destroy'
         ./spec/controllers/users_controller_spec.rb:326:in `block (4 levels) in <top (required)>'

users_controller.rb

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

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

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

  def new
  @user = User.new
  @title = "Sign up"      
  end

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

  def edit
    @title = "Edit user"
  end

  def update
    if @user.update_attributes(params[:user]) 
      redirect_to @user, :flash => { :success => "Profile updated." }
    else
    @title = "Edit user"
    render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    redirect_to users.path, :flash => { :success => "User destroyed." }
  end

  private

    def authenticate
      deny_access unless signed_in?
    end

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

    def admin_user
      @user = User.find(params[:id])
      redirect_to(root_path) if !current_user.admin? || current_user?(@user)
    end
end

Users_controller_spec.rb:

    describe "DELETE 'destroy'" do

      before(:each) do
        @user = Factory(:user)
      end

      describe "as a non-signed-in user" do
        it "should deny access" do
          delete :destroy, :id => @user
          response.should redirect_to(signin_path)
        end
      end

      describe "as non-admin user" do
        it "should protect the action" do
          test_sign_in(@user)
          delete :destroy, :id => @user
          response.should redirect_to(root_path)
        end
      end

      describe "as an admin user" do

        before(:each) do
          @admin = Factory(:user, :email => "admin@example.com", :admin => true)
          test_sign_in(@admin)
        end

        it "should destroy the user" do
          lambda do
            delete :destroy, :id => @user
          end.should change(User, :count).by(-1)
        end

        it "should redirect to the users page" do
          delete :destroy, :id => @user
          flash[:success].should =~ /destroyed/i
          response.should redirect_to(users_path)
        end

        it "should not be able to destroy itself" do
          delete :destroy, :id => @admin
          lambda do
            delete :destroy, :id => @admin
          end.should_not change(User, :count)
        end
      end
  end   
end

我是初学者,但总是习惯于在此处发布任何问题之前进行广泛的研究(90%的时间我通过搜索SO和Google找到答案)。

我最后2个未发布的问题(在发布之前找到了答案)我通过更新我的Gemfiles(rspec中的will_paginate和undefined _selector)来修复。这可能也是吗?

感谢您的关注并花时间阅读本文。

1 个答案:

答案 0 :(得分:5)

找到解决上述问题的拼写错误(duh!):

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

而不是“users_path”我做了“users.path”

尝试从浏览器中删除时注意到它。