Rails 画像アップロードのテスト
photos_attributes: [:image]に対するテストをしたいのですが、調べてもわかりませんでした。助けていただけると幸いです。
失敗時のテストは成功しました。
現状
post.controller.rb def create @post=Post.new(post_params) if @post.photos.present? @post.save redirect_to root_path flash[:notice] = "投稿が保存されました" else redirect_to root_path flash[:alert] = "投稿に失敗しました" end end private def post_params params.require(:post).permit(:caption, photos_attributes: [:image]).merge(user_id: current_user.id) end
テスト
test "upload post should not be correct without photo" do get new_post_path assert_select 'div.card-header' post posts_path, params: { post: { caption: "first upload", user_id: @user.id } } assert_redirected_to root_path assert_equal "投稿に失敗しました", flash[:alert] end test "upload pic shoud be correct" do get new_post_path assert_select 'input[type="file"]' image = fixture_file_upload('test/fixtures/rails.png', 'image/png') post posts_path, params: { post: { caption: "first upload", user_id: @user.id, photos_attributes: {image: image } } } assert_redirected_to root_path assert_equal "投稿が保存されました", flash[:alert] end
試したこと
photos_attributes: {image: image }に考えられる値を入れたが失敗した
debug
1Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"caption"=>"jk", "photos_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007fb216c03ee0
補足情報(FW/ツールのバージョンなど)
minitest使用 rails6.0.3
あなたの回答
tips
プレビュー