- rails
- rspec
でrequest specにてpostした際にちゃんと保存されているのかをテストしたいと考えています。
その際下記で書いているのですが、うまく保存されないです。おそらくparamsの書き方が良くないんだと思うのですがexpect内でpostする際のparamsの書き方を教えていただけないでしょうか?
describe "plans/create" do let!(:user_01) { FactoryBot.create(:user) } let!(:plan_of_user01) { FactoryBot.create(:plan, user: user_01) } example "planを保存できるか" do plan_params = { name: "テストプラン", description: "テストプランのdescription", price: 9800, stripe_product_id: "dwqbjdbqwodnq" } expect { post '/plans', params: plan_params }.to change { Plan.count }.from(1).to(2) end end
ストロングパラメーターで下記で設定しています
def plan_params params.require(:plan).permit(:name, :price) end
発生している問題・エラーメッセージ
上記コードでテストすると下記のエラーが出ます
Failures: 1) Plans plans/create planを保存できるか Failure/Error: expect { post '/plans', params: plan_params }.to change { Plan.count }.from(1).to(2) expected `Plan.count` to have changed from 1 to 2, but did not change # ./spec/requests/plans_spec.rb:63:in `block (3 levels) in <top (required)>'
試したこと
うまく保存ができていなかったのでパラメーターを確認
投稿時に下記パラメーターで保存されているのを確認
Parameters: {"authenticity_token"=>"", "plan"=>{"name"=>"test", "price"=>"9800", "description"=>"テスト", "plan_category_id"=>"1", "request"=>"true", "trial"=>"false", "user_id"=>"1"}, "commit"=>"登録する"}
paramsの定義を下記に直した
let!(:plan_params) { { plan: { id: 1, name: "テストプラン", description: "テストプランのdescription", price: 9800, stripe_product_id: "dqdqwdqw", stripe_price_id: "dwqnjdnqiwdnq", request:true, public:true, trial: true, rating: 3, user_id: user_01.id, plan_category_id: 1 } } }
しかしPlan.createした際に下記のエラー
*** ActiveModel::UnknownAttributeError Exception: unknown attribute 'plan' for Plan.
実際に投稿する際には下記のパラーメーターなのでなんでplanが認識されないのかわからず。。。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/11 01:34
2023/01/11 02:22
2023/01/17 00:26