特征
用户有一个配置文件并且应该能够更新它。
问题
我更新了配置文件,例如将名称更改为“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
用户设置为 - 根据其他 Stackoverflow 线程,我没有
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
我曾尝试在此 Stackoverflow 线程中@user
进行交换: “设计 Rspec 注册控制器测试在更新时失败,就好像它正在尝试确认电子邮件地址一样”subject.current_user
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
而不是通过找到用户 via params[:id]
。
注册控制器.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
助手
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
我的集成规范通过了。我在控制器中缺少什么?