Rails 5测试错误来自find过滤器中的params [:id]

时间:2017-04-11 21:50:00

标签: ruby-on-rails ruby ruby-on-rails-5 best-in-place

我在Rails 5中编写了一个应用程序,其中包含了最好的宝石'用于更新操作视图。即使应用程序工作正常并更新正确的商品的正确参数,测试套件也会抛出此错误:

ActiveRecord::RecordNotFound: Couldn't find Offer with 'id'=
app/controllers/offers_controller.rb:71:in `correct_user'

这是有问题的代码。它是更新操作的前过滤器:

70: def correct_user
71:     @offer = Offer.find params[:id]
72:     @user = @offer.user
73:     redirect_to(root_url) unless current_user?(@user)
    end

但PUT显然将id参数定义为id =' 109':

Started PUT "/offers/109" for 127.0.0.1 at 2017-04-11 17:21:33 -0400
Processing by OffersController#update as JSON
Parameters: {"offer"=>{"category"=>"d"},     "authenticity_token"=>"9Y94wYzCrCvyygNk5GJXRe488JPd51YCSWQLe6Lpo3MgglfK1SrI1NcEj334T9HaAkAazlS212i4FZ+akDyelg==", "id"=>"109"}

这是我在测试中使用的商品夹具:

one:
   category: MyText
   plan_name: MyString
   price: $109
   rate: Month
   plan_customer_satisfaction: 1
   user_id: users(:michael).id

用户夹具:

michael:
  name: Michael Example
  email: michael@example.com
  username: mexample
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: Time.zone.now

这是测试:

def setup
   @user = users(:michael)
   @offer = offers(:one)
end

test "should update when logged in" do
   log_in_as(@user)
   patch offers_update_path(@offer), params: { offer: { category: 'Heating',
                                           plan_name: 'Synergy',
                                           price: '$120',
                                           rate: 'Month' } }
   assert_response :success
end

如果您需要更多信息,请告诉我们。任何帮助表示赞赏。感谢。

0 个答案:

没有答案