【发布时间】:2013-09-25 14:31:50
【问题描述】:
特点
用户拥有个人资料并且应该能够对其进行更新。
问题
我更新了配置文件,例如将名称更改为“Homer Simpson”,但所有断言都失败了,因为数据库记录似乎没有更新。
我似乎无法获得更新的属性:
Failure/Error: expect(subject.current_user.first_name).to eq('Homer')
expected: "Homer"
got: "Lew"
(compared using ==)
# ./spec/controllers/registrations_controller_spec.rb:67:in `block (3 levels) in <top (required)>'
注意@user.reload和subject.current_user.reload我都试过了
规范仍未通过。
代码
我正在使用:
- 导轨 (4.0.0)
- 设计 (3.0.3)
- rspec-rails (2.14.0)
- 水豚 (2.1.0)
- factory_girl (4.2.0)
- database_cleaner (1.1.1)
我已经检查过了:
- 我已将
devise.mapping设置为用户 - 我没有像 per this other Stackoverflow thread 那样污染设计会话的
valid_session。
registrations_controller_spec.rb
describe "User Profiles" do
login_user
it "Update - changes the user's attributes" do
put :update, id: @user, user: attributes_for(:user, first_name: 'Homer')
@user.reload
expect(@user.first_name).to eq('Homer') # FAILS
end
end
我尝试将@user 换成subject.current_user,就像在这个 Stackoverflow 线程中一样:"Devise Rspec registration controller test failing on update as if it was trying to confirm email address"
put :update, id: subject.current_user, user: attributes_for(:user, first_name: 'Homer')
subject.current_user.reload
expect(subject.current_user.first_name).to eq('Homer') # Still FAILS
但还是失败了。
控制器有问题吗?我通过current_user.id而不是params[:id]找到用户。
registrations_controller.rb
def update
@user = User.find(current_user.id)
email_changed = @user.email != params[:user][:email]
password_changed = !params[:user][:password].blank?
if email_changed or password_changed
successfully_updated = @user.update_with_password(user_params)
else
successfully_updated = @user.update_without_password(user_params)
end
if successfully_updated
sign_in @user, bypass: true # Sign in the user bypassing validation in case his password changed
redirect_to user_profile_path, notice: 'Profile was successfully updated.'
else
render "edit"
end
end
controller_macros.rb - 定义login_user helper
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = FactoryGirl.create(:user)
@user.confirm!
sign_in @user
end
end
end
我的集成规范通过了。我在控制器中缺少什么?
【问题讨论】:
标签: ruby-on-rails rspec devise controller