mercredi 9 septembre 2015

Rails 3.2 reload! not working as expected

I am trying to test a create action on my users controller. The ask is to allow customers who have previously created accounts, but never actually created a subscription, to use the same email at signup but not be bound to their original password. My test is as follows:

 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

my controller looks like:

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

If I put a binding in below the

user.user_entitlements.empty? && user.update_attributes(password: params[:password], password_confirmation: params[:password_confirmation])

and I call user.password I get "mynewpassword" so the password is updating. in the test though the password is still showing as the original password. I tried adding user6.reload! in the test before the expectation block and I get

NoMethodError: undefined method `reload!' for #<User:0x007fa9a3342fc0>

I found this issue: http://ift.tt/1LkiD6q which suggests that I should modify my .pryrc file. I didn't have a .pryrc file previously. I created a .pryrc file and tried everything in this post(pry gem how to reload?) one at a time with no success. I created the .pryrc in the root of the app and at this point I am at a loss as to what to do.

Aucun commentaire:

Enregistrer un commentaire