前提
Rspecでてすとをしたいのですがうまくいきません。
エラーがどうやったらなおりますか?
実現したいこと
factorybotでテストデータを作る際、関連テーブルのuserとpostも自動生成するようにする
発生している問題・エラーメッセージ
ActiveRecord::RecordInvalid: Validation failed: User must exist, Post must exist
該当のソースコード
factorybots
1FactoryBot.define do 2 factory :good do 3 association :post 4 association :user 5 sequence(:post_id) { |n| "#{n}_post"} 6 sequence(:user_id) { |n| "#{n}_user"} 7 end 8end
factorybots
1FactoryBot.define do 2 factory :post do 3 user_id { 'test_user' } 4 title { 'testtitle' } 5 end 6end
factorybots
1FactoryBot.define do 2 factory :user do 3 name { 'test_user' } 4 end 5end 6
model
1class Like < ApplicationRecord 2 belongs_to :user 3 belongs_to :post 4end 5
rspec
1require 'rails_helper' 2 3RSpec.describe "Goods", type: :request do 4 describe "GET /index" do 5 it '全てのいいねデータを取得する' do 6 FactoryBot.create_list(:good, 10) 7 8 get '/goods' 9 json = JSON.parse(response.body) 10 11 expect(response.status).to eq(200) 12 expect(json.length).to eq(10) 13 end 14 end 15end
試したこと
associationを二つ並べるだけではダメなんでしょうか?
associationとsecenceの両方書いてるのは何故でしょう

回答1件
あなたの回答
tips
プレビュー