3

当您希望允许用户在不提供密码的情况下编辑他们的帐户时,我遵循了这个设计 wiki 文档,了解如何为注册控制器编写自定义更新操作,除非他们自己更改密码。 设计 Wiki - 如何允许用户在不提供密码的情况下编辑帐户。

但是,我无法弄清楚我的 Rspec 测试中缺少什么以使其通过。以下是相关的代码片段:

应用程序/控制器/registrations_controller.rb

def update
  @user = User.find(current_user.id)

  successfully_updated = if needs_password?(@user, params)
    @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
  else
    # remove the virtual current_password attribute
    # update_without_password doesn't know how to ignore it
    params[:user].delete(:current_password)
    @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
  end

  if successfully_updated
    set_flash_message :notice, :updated
    # Sign in the user bypassing validation in case their password changed
    sign_in @user, :bypass => true
    redirect_to users_path
  else
    render "edit"
  end
end

规格/工厂/users.rb

FactoryGirl.define do
  factory :user do
    email         { Faker::Internet.email }
    password      'XXXXXXXXX'
    first_name    { Faker::Name.first_name }
    middle_name   { Faker::Name.first_name }
    last_name     { Faker::Name.last_name }
  end
end

规范/控制器/registrations_controller_spec.rb

describe "PUT #update" do
  login_pcp

  let(:user) { FactoryGirl.create(:user, first_name: 'Tom') }

  it "changes user attributes" do
    attrs = FactoryGirl.attributes_for(:user, first_name: 'Jerry')
    attrs.delete(:password)
    put :update, user: attrs
    user.reload
    assigns[:user].should_not be_new_record
    expect(user.first_name).to eq 'Jerry'
    expect(flash[:notice]).to eq 'You updated your account successfully.'
  end
end

当我运行规范时,我收到以下错误:

Failures:

1) RegistrationsController PUT #update changes user attributes
   Failure/Error: expect(user.first_name).to eq 'Jerry'

   expected: "Jerry"
        got: "Tom"

   (compared using ==)
 # ./spec/controllers/registrations_controller_spec.rb:55:in `block (3 levels) in <top (required)>'

出于某种原因,它没有保存更新。我不确定是否应该输入密码才能进行更新?任何帮助,将不胜感激。谢谢!

4

2 回答 2

1

测试现在看起来像这样并且它通过了:

describe "PUT #update" do
  before :each do
    @request.env['devise.mapping'] = Devise.mappings[:user]
    user_tom = FactoryGirl.create(:user, email: 'tom@test.com')
    sign_in user_tom
  end

  it "changes user attributes" do
    put :update, user: { email: 'jerry@test.com' }
    subject.current_user.reload
    assigns[:user].should_not be_new_record
    expect(subject.current_user.email).to eq 'jerry@test.com'
    expect(flash[:notice]).to eq 'You updated your account successfully.'
  end
end
于 2015-06-03T19:41:22.913 回答
0

我也遇到了这个问题,但我可以看到这是因为当您填写更新表单时,您将需要填写一个名为“当前密码”的字段。因为除非您填写该字段,否则数据不会更新。当你使用 factory girl 生产用户数据时,没有这个值。我解决了它,如以下代码所示。

describe "PATCH #UPDATE" do before :each do @user = create(:user) @old_email = @user.email sign_in @user end context 'valid attributes' do it "updates user attributes" do patch :update, id: @user, user: attributes_for(:user, current_password: "password") expect(@user.reload.email).not_to eq(@old_email) end end end

于 2015-11-02T00:21:14.093 回答