phron
phron

Reputation: 1845

Mock expectation error... Don't understand why this test fails (all works fine in browser)

I have the "updates the requested user" part of my test that fails and I cannot understand why.

 describe "PUT/PATCH #update_profile" do

    context "with valid params" do
      it "updates the requested user" do
        user = create(:john_doe)
        # Assuming there are no other users in the database, this
        # specifies that the User created on the previous line
        # receives the :update_attributes message with whatever params are
        # submitted in the request.
        User.any_instance.should_receive(:update_profile).with({identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}})
        put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}}}

      end

      it "assigns the requested user as @user" do
        user = create(:john_doe)
        put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}} }
        expect(assigns(:user)).to eq(user)
      end

      it "redirects to the user" do
        user = create(:john_doe)
        put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}}}

        expect(response).to redirect_to foundry_users_url
      end
    end

the 2 other parts (assigns and redirect) pass fine, and all works as expected when testing in browser.

The error message is "RSpec::Mocks::MockExpectationError: Exactly one instance should have received the following message(s) but didn't: update_profile"

EDIT : I add here the users controller (I keep here only the relevant parts of code: create action update action (for reference) and update_profile action (which causes the spec fail). Remember that only this spec is failing, all other works as expected, the problem is just in the way I wrote the test.

User has_one :identity and accepts_nested_attributes_for :identity

class Foundry::UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :edit_profile, :update_profile, :destroy]

def create  
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to foundry_users_url, flash: {success: "User was successfully created."} }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

def update
    respond_to do |format|
      if @user.update(user_params_for_update)
        format.html { redirect_to foundry_users_url, notice: 'Credentials were successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

def update_profile
    respond_to do |format|
      if @user.update(user_params_for_update_profile)

        format.html { redirect_to foundry_users_url, notice: 'Profile was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit_profile' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

def user_params
    # used only on creation
    params.require(:user).permit(:email, identity_attributes: [:last_name, :first_name])
  end

  def user_params_for_update
    # used only on 'regular' update action -- updates only credentials that are user's attributes
    params.require(:user).permit(:email, :password, :password_confirmation)
  end

  def user_params_for_update_profile
    # used only on update_profile action (later should have identity_attributes, addresses_attributes, and some others...) 
    params.require(:user).permit(identity_attributes: [:last_name, :first_name, :email_rescue, :dob, :bio, :gender, :id])
  end

I suppose I'm doing something wromg somewhere but I cannot see where and why...

Thanks for your help

Upvotes: 0

Views: 7162

Answers (2)

phron
phron

Reputation: 1845

I have it to work ! Thanks @DNNX who puts me on the right direction the problem as expected was in the way I wrote the test, user.any_instance should receive :update_profile instead of update. I put here the passing spec for information..

  describe "PUT/PATCH #update_profile" do
    context "with valid params" do
      it "updates the requested user" do
        user = create(:john_doe)
        User.any_instance.should_receive(:update_profile).with({"identity_attributes"=>{"last_name" => "BIDON", "first_name" => "Bidon", "dob" => "1970-07-15"}})

        put :update_profile, {:id => user.to_param, :user => {:identity_attributes =>{last_name: 'BIDON', first_name: 'Bidon', dob: "1970-07-15"}}}
      end

      it "assigns the user as @user" do
        user = create(:john_doe)
        # Trigger the behavior that occurs when valid params are submitted
        User.any_instance.stub(:update_profile).and_return(true)
        put :update_profile, {:id => user.to_param, :user => { identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15"}}}
        expect(assigns(:user)).to eq(user)
      end

      it "redirects to users list" do
        user = create(:john_doe)
        # Trigger the behavior that occurs when valid params are submitted
        User.any_instance.stub(:update_profile).and_return(true)
        put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15"}}}
        expect(response).to redirect_to foundry_users_url
      end

    end

    context "with invalid params" do
      it "assigns the user as @user" do
        user = create(:john_doe)
        # Trigger the behavior that occurs when invalid params are submitted
        User.any_instance.stub(:update_profile).and_return(false)
        put :update_profile, {:id => user.to_param, :user => { identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id" => user.identity.id}}}
        expect(assigns(:user)).to eq(user)
      end

      it "re-renders the 'edit_profile' template" do
        user = create(:john_doe)
        # Trigger the behavior that occurs when invalid params are submitted
        User.any_instance.stub(:update_profile).and_return(false)
        put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id" => user.identity.id}}}
        expect(response).to render_template :edit_profile
      end
    end
  end

the rest of code posted in my question still the same.. and now, all the tests are green

EDIT as said in to DNNX in comments I forgot to mention the essential modif to the controller itself, I put it here :

 def update_profile
    respond_to do |format|
      if @user.update_profile(user_params_for_update_profile) // changed to call update_profile instead of update !
      // rest of code still the same

Cheers

Upvotes: 2

DNNX
DNNX

Reputation: 6255

Your controller doesn't call update_profile on any instance of User. It calls User#update. Try this:

User.
  any_instance.
  should_receive(:update).
  with(identity_attributes: {
         "last_name" => "Bidon",
         "first_name" => "Bidon",
         "dob" => "1970-07-15",
         "id" => user.identity.id})

Or this if the code above doesn't work:

User.any_instance.should_receive(:update)

Upvotes: 1

Related Questions