2013-03-10 87 views
0

我有一些麻烦来测试我的用户模型上覆盖的设计的update_with_password方法。RSpec +设计+覆盖update_with_password

我使用Rails 3.2,设计2.2.3和RSpec护栏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 

我该如何测试这种方法?有些东西我错过了,我无法弄清楚。提前致谢。

回答

1

我觉得你只是想

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

,而不是

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

谢谢本,它的工作! – edelpero 2013-03-10 12:56:02