0

我在从我在我的用户模型上覆盖的设计中测试 update_with_password 方法时遇到了一些麻烦。

我正在使用 Rails 3.2、Devise 2.2.3 和 Rspec-rails 2.13

这是我的用户模型 update_with_password 方法的样子:

#app/model/user.rb

def update_with_password(params, *options)
  if encrypted_password.blank?
    update_attributes(params, *options)
  else
    super
  end
end

这是我的规格:

#spec/model/user_spec.rb

describe "#update_with_password" do
  context "when encrypted password is blank" do

    before(:each) do
      @user = FactoryGirl.build(:user_via_facebook)
      @user.save(validate: false)
      @facebook_profile = FactoryGirl.create(:facebook_profile, user: @user)
    end

    it "updates user attributes without asking current password" do
      @user.update_with_password(params: {first_name: "New First Name"})
      @user.first_name.should eql "New First Name"
    end

  end
end

这是我在运行规范后遇到的错误:

User#update_with_password when encrypted password is blank updates user attributes without asking current password
  Failure/Error: @user.update_with_password(params: {first_name: "New First Name"})
    ActiveModel::MassAssignmentSecurity::Error:
      Can't mass-assign protected attributes: params

我应该如何测试这种方法?我缺少一些东西,我无法弄清楚。提前致谢。

4

1 回答 1

1

我想你只是想要

@user.update_with_password({first_name: "New First Name"})

代替

@user.update_with_password(params: {first_name: "New First Name"})
于 2013-03-10T01:06:17.740 回答