rails 4.0.3 rspec 3.0.2 undefined method' sign_in'错误

时间:2014-08-28 07:33:53

标签: ruby-on-rails rspec

此测试失败

describe "forbidden attributes", type: :request do

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

      let(:params) do

        { user: { zero_cost: true, password: user.password, password_confirmation: user.password } }

      end

      before do

        sign_in user, no_capybara: true

        patch user_path(user), params

      end

      specify { expect(user.reload).not_to be_zero_cost }

    end

我的github的链接是here的完整代码

以前是为admin boolean table列运行的,但我将其切换到另一列,看看测试是否仍会失败,同样的错误。

用户控制器

class UsersController < ApplicationController

  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]

  before_action :correct_user, only: [:edit, :update]

  before_action :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(user_params)

    if @user.save

      sign_in @user

      flash[:success] = "Welcome to the Sample!"

      redirect_to @user

    else

      render 'new'

    end

  end

  def edit
  end

  def update

    if @user.update_attributes(user_params)

      flash[:success] ="Profile updated"

      redirect_to @user

    else

      render 'edit'

    end

  end

  def destroy

    User.find(params[:id]).destroy

    flash[:success] = "User deleted."

    redirect_to users_url

  end

  private

  def user_params

      params.require(:user).permit(:name, :email, :password, :password_confirmation)

  end

  # before filters

  def signed_in_user

    unless signed_in?

      store_location

      redirect_to signin_url, notice: "Please sign in."

    end

  end

  def correct_user

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

    redirect_to(root_url) unless current_user?(@user)

  end

  def admin_user

    redirect_to(root_url) unless current_user.admin?

  end

end

3 个答案:

答案 0 :(得分:1)

从测试中删除sign_in:

describe "forbidden attributes" do
  let(:user) { FactoryGirl.create(:user) }

  before do
    patch :update, id: user.id, user: { zero_cost: true, password: user.password, password_confirmation: user.password }
  end

  specify {
    expect(assigns(:user).zero_cost).to be_true
    expect(response).to redirect_to(user_path(assigns(:user).id))
  }
end

答案 1 :(得分:0)

您必须创建方法&#39;登录&#39; in&#39; spec / support / utilities.rb&#39;这样你就可以在测试中访问它。

您可以确认此项检查文件&#39; utilities.rb&#39;在Michael Hartl的GitHub示例App:

https://github.com/mhartl/sample_app_4_0_upgrade/blob/master/spec/support/utilities.rb

答案 2 :(得分:0)

rails指南最初将此测试放在描述编辑部分,但是here(解决方案18305598)建议将其与“用户”##测试

删除之前块中的sign_in用户是正确的

这是经过纠正的工作测试

   describe "forbidden attributes", type: :request do

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

      let(:params) do

        { user: { admin: true, password: user.password, password_confirmation: user.password } }

      end

      before do

        patch user_path(user), params

      end

      specify { expect(user.reload).not_to be_admin }

    end

必须运行脚本顶部的这段代码

before(:each) do
  sign_in user
  visit users_path
end

意味着我无法使用此命令运行单个测试

bundle exec rspec spec/features/user_pages_spec.rb -e "forbidden attributes"

因为它需要用户登录