accepts_nested_attributes_forを使った時のrequest specでのパラメータ指定

問題

menuを作成するrequest specでパラメータ の指定の仕方がおかしかったようで、テストが落ちておりました。

menuモデルでは,

accepts_nested_attributes_for :menu_images

というようにmenu_imagesを子レコードとして作成するようにしておりました。

 

以下は落ちてしまっていたテストです。

context '店舗ログイン' do
it "create" do
sign_in no_menu_restaurant.restaurant_user
post restaurant_menus_path, params: { menu: FactoryBot.attributes_for(:menu, :with_menu_images, restaurant_id: no_menu_restaurant.id) }
expect(response).to have_http_status(302)
expect(response.location).to eq(restaurant_menu_url(Menu.find_by(restaurant: no_menu_restaurant).id))
end
end
end

  

テスト実行結果

 (modify/menu_image_create|✚1) [1] $

docker-compose run --rm app bundle exec rspec spec/requests/restaurant/restaurant_menus_spec.rb

 

Failures:

 

  1) Restaurant::Menus POST /restaurant/menus/ 店舗ログイン create

     Failure/Error: expect(response).to have_http_status(302)

       expected the response to have status code 302 but it was 200

     # ./spec/requests/restaurant/restaurant_menus_spec.rb:133:in `block (4 levels) in <top (required)>'

 

Finished in 23.46 seconds (files took 6.76 seconds to load)

18 examples, 1 failure

 

Failed examples:

 

rspec ./spec/requests/restaurant/restaurant_menus_spec.rb:124 # Restaurant::Menus POST /restaurant/menus/ 店舗ログイン create

 

 

 きちんとmenuがcreateされればリダイレクトされるはずなのになぜかステータスが200になってしまっておりました。

コントローラでmenuがsaveされなかったら render new されるようにしていたので、200が返って来ておりました。

 

 

 

 

パラメータを確認してみたら、どうやらネストさせているmenu_images_attributesのパラメータがうまくFactoryBotで作成できておりませんでした。。。

 

 

 

 

解決方法 

以下のように、

image_params = { menu_images_attributes: [ FactoryBot.attributes_for(:menu_image) ] }

とネストされるパラメータを作成してから、それをmergeするように書き換えてあげたら無事テストが通るようになってくれました!!

context '店舗ログイン' do
it "create" do
sign_in no_menu_restaurant.restaurant_user
image_params = { menu_images_attributes: [ FactoryBot.attributes_for(:menu_image) ] }
post restaurant_menus_path, params: { menu: FactoryBot.attributes_for(:menu, :with_menu_images, restaurant_id: no_menu_restaurant.id).merge(image_params) }
p params: { menu: FactoryBot.attributes_for(:menu, :with_menu_images, restaurant_id: no_menu_restaurant.id).merge(image_params) }
expect(response).to have_http_status(302)
expect(response.location).to eq(restaurant_menu_url(Menu.find_by(restaurant: no_menu_restaurant).id))
end
end
end

 

 

(modify/menu_image_create|✚1) [1] $

docker-compose run --rm app bundle exec rspec spec/requests/restaurant/restaurant_menus_spec.rb

Starting flatea_redis_1 ... done

Starting flatea_db_1    ... done

...........{:params=>{:menu=>{:name=>"menu_10", :description=>"description_10", :price=>5000, :restaurant_id=>2094, :menu_images_attributes=>[{:image=>#<Rack::Test::UploadedFile:0x000055c00b508d60 @original_filename="sample.jpg", @tempfile=#<Tempfile:/tmp/sample20190516-1-gi60xt.jpg>, @content_type="text/plain">}]}}}

.......

 

Finished in 24.28 seconds (files took 7.61 seconds to load)

18 examples, 0 failures

 

 

以下の記事を参考にさせていただきました!!!

 

qiita.com