2

I am very new to rspec and factory girl and I am stuck with a strange problem. I have an action in a controller like:

   def update
      @property = current_user.properties.find params[:fee][:property_id]
      @fee = @property.fees.find(params[:id])
     if @fee.update_attributes(params[:fee])
       redirect_to fee_path(:prop=>@property), :notice => "fee updated successfully!"
     else
       render action: "edit"
     end
   end

and a test example:

    describe "with valid params" do
      before do
        @property = FactoryGirl.create(:property)
        @property.users << subject.current_user
      end
      it "updates the requested fee" do
        fee = @property.fees.create! valid_attributes
        Fee.any_instance.should_receive(:update_attributes).with({ "name" => "MyString","property_id"=>@property.id})
        put :update, {:id => fee.to_param, :fee => { "name" => "MyString","property_id"=>@property.id }}, valid_session
      end
    end

but I get a strange error:

    #<Fee:0xb8c4884> received :update_attributes with unexpected arguments
     expected: ({"name"=>"MyString", "property_id"=>"50ec0b3fa7c320ee53000002"})
          got: ({"name"=>"MyString", "property_id"=>"50ec0b3fa7c320ee53000002"})

If anyone can help I'll be very thankful.

Asnad Atta
  • 3,855
  • 1
  • 32
  • 49

1 Answers1

1

So I can see a few reasons for this, but my best bet is, try to freeze the hash in a separate var and see if it works, something like this:

  describe "with valid params" do

      let(:property) { FactoryGirl.create(:property) }

      let(:params) do
        {"name" => "MyString","property_id"=> property.id}
      end

      before do
        property.users << subject.current_user
      end

      it "updates the requested fee" do
        fee = property.fees.create! valid_attributes
        Fee.any_instance.should_receive(:update_attributes).with(params)
        put :update, {:id => fee.to_param, :fee => params}, valid_session
      end
    end
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73