收到:带有意外参数的update_attributes

时间:2013-01-08 12:25:54

标签: ruby-on-rails rspec mongoid factory-bot

我对rspec和工厂女孩很新,我遇到了一个奇怪的问题。我在控制器中有一个动作,如:

   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

和一个测试示例:

    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

但我收到一个奇怪的错误:

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

如果有人可以提供帮助,我会非常感激。

1 个答案:

答案 0 :(得分:1)

所以我可以看到一些原因,但我最好的选择是,尝试将哈希冻结在一个单独的var中,看看它是否有效,如下所示:

  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