こんにちは!
Carrierwaveを使って画像付きの記事投稿サービスを作りたいと思っています。
今RSpecのところでつまづいているため質問させていただきました。
前提・実現したいこと
モデルの関連付け
・User(親)=>Content(子)=>Image(孫)
CarrierwaveをContentモデルとImageモデルにそれぞれ適応しています。
Contentモデルの要素
title thumbnail(サムネイル画像) <= Carrierwaveを適用しています description(本文) user_id
Imageモデルの要素
image <= Carrierwaveを適用しています content_id
ここで質問なのですが、RSpecのrequestスペックで
ContentsモデルとネストされたImageモデルのレコードをテストしたいのですが、
Contentモデルはレコードされるのに、Imageモデルがレコードできないです。
アプリ上では正常にレコードされます。
なのでRSpecの書き方に問題があると思っています。
なにかいい方法があればご教授ください。
発生している問題・エラーメッセージ
expected #count to have changed by 1, but was changed by 0
該当のソースコード
Rspec
describe "コンテンツの投稿成功" do before do @user = FactoryBot.create(:user) @content_attributes = FactoryBot.attributes_for(:content) # ハッシュオブジェクトの生成 login_as @user end context "タイトル、サムネ、画像、本文が正常に入力されている時" do it "コンテンツが投稿される" do @content_attributes = FactoryBot.attributes_for(:content) expect { post create_content_path, params: { content: @content_attributes, images_attributes: @image_attirbutes } }.to change(Content, :count).by(1).and change(Image, :count).by(1) follow_redirect! expect(response).to be_success end end end
ContentsController
# GET /contents/new def create @content = current_user.contents.build(content_params) if @content.save flash.now[:success] = "Success Posting Content" redirect_to content_path(@content.id) else flash.now[:danger] = "Error Posting Content" render "new" end end private def content_params params.require(:content).permit(:title, :thumbnail, :description, :thumbnail_image, images_attributes: [:image]) end
Factory/contents
FactoryBot.define do factory :content do title "タイトルc" thumbnail {Rack::Test::UploadedFile.new(File.join(Rails.root,'spec/fixtures/images/thum.jpg'))} description "Rspec本文" association :user end end
Factory/images
FactoryBot.define do factory :image do image {Rack::Test::UploadedFile.new(File.join(Rails.root,'spec/fixtures/images/image1.jpg'))} association :content end end
あなたの回答
tips
プレビュー