2

我对 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"})

如果有人可以提供帮助,我将不胜感激。

4

1 回答 1

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
于 2013-02-12T03:31:27.493 回答