【发布时间】:2015-09-09 16:11:26
【问题描述】:
我正在尝试在我的用户控制器上测试创建操作。要求是允许以前创建帐户但从未实际创建订阅的客户在注册时使用相同的电子邮件,但不受其原始密码的约束。我的测试如下:
it "updates the user password for user with no entitlement" do
user6 = Factory(:user)
user_params_without_entitlement= { :email => user6.email, :password => "mynewpassword", :password_confirmation => "mynewpassword", publisher_id: user6.publisher_id }
post :create, user_params_without_entitlement
hash = Hashie::Mash.new(JSON.parse response.body)
expect(hash[:errors].present?).to eq(false)
expect(user6.password).to eq("mynewpassword")
end
我的控制器看起来像:
def create
if @user.save && @user.activate!
render :create, status: 200
elsif @user.errors[:email].first == "Oops! Looks like you've already created an account. Please <a href='/account'>click here to sign in.</a>"
user = User.find_by_email(params[:email])
if user.user_entitlements.empty? && user.update_attributes(password: params[:password], password_confirmation: params[:password_confirmation])
render :create, status: 200
else
render json: {errors: @user.errors}, status: 422
end
else
if render json: {errors: @user.errors}, status: 422
end
end
end
如果我在下面放置一个绑定
user.user_entitlements.empty? && user.update_attributes(password: params[:password], password_confirmation: params[:password_confirmation])
然后我调用 user.password 我得到“mynewpassword”所以密码正在更新。在测试中虽然密码仍然显示为原始密码。我尝试添加 user6.reload!在期望块之前的测试中,我得到了
NoMethodError: undefined method `reload!' for #<User:0x007fa9a3342fc0>
我发现了这个问题:https://github.com/rweng/pry-rails/issues/9,这表明我应该修改我的 .pryrc 文件。我以前没有 .pryrc 文件。我创建了一个 .pryrc 文件,并一次尝试了这篇文章 (pry gem how to reload?) 中的所有内容,但没有成功。我在应用程序的根目录中创建了 .pryrc,此时我不知道该怎么做。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rspec pry