rspec specを実行したところ以下のエラーが出ました
Post Validations creates successfully
Failure/Error: expect(post).to be_valid
expected #<Post id: nil, title: "aaaaaaaaaa", content: "aaaaaaaaaaaaaaaaaaaa", category_id: nil, user_id: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Category can't be blank
関係ありそうな箇所を載せます
spec/models/post_spec.rb context 'Validations' do let(:post) { build(:post) } it 'creates successfully' do expect(post).to be_valid end it 'is not valid without a category' do post.category_id = nil expect(post).not_to be_valid end it 'is not valid without a title' do post.title = nil expect(post).not_to be_valid end it 'is not valid without a user_id' do post.user_id = nil expect(post).not_to be_valid end it 'is not valid with a title, shorter than 5 characters' do post.title = 'a' * 4 expect(post).not_to be_valid end it 'is not valid with a title, longer than 255 characters' do post.title = 'a' * 260 expect(post).not_to be_valid end it 'is not valid without a content' do post.content = nil expect(post).not_to be_valid end it 'is not valid with a content, shorter than 20 characters' do post.content = 'a' * 10 expect(post).not_to be_valid end it 'is not valid with a content, longer than 1000 characters' do post.content = 'a' * 1050 expect(post).not_to be_valid end end
spec/factories/posts.rb FactoryBot.define do factory :post do title {'a' * 10} content {'a' * 20} user category end end
spec/factories/categories.rb FactoryBot.define do factory :category do sequence(:name) { |n| "name#{n}" } sequence(:branch) { |n| "branch#{n}" } end end
categoryがnilなのがダメかと思いfactoryの内容を変更してみたりしましたが分かりませんでした。何かアドバイスあれがお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。