以下のような構成で利用しているModelのテストを行いたいですが
FactoryBotを利用した疑似データの生成がうまくいっていないのか
Category idsがブランクとエラーになります。
この部分の切り分け対応のノウハウがなくどなたか対応方法をご教授いただければと存じます。
Model構成
CategoriesArticleが中間テーブルで多対多の設定を行っています。
Articles←→CategoriesArticle←→Categories
ArticleModelでは以下の通りcategory_idsが登録されているかvalidatesを設定しています
class Article < ActiveRecord::Base validates :category_ids, presence: true has_many :categories_articles has_many :categories, through: :categories_articles end
class CategoriesArticle < ActiveRecord::Base belongs_to :category belongs_to :article end
class Category < ActiveRecord::Base has_many :categories_articles has_many :articles, through: :categories_articles accepts_nested_attributes_for :categories_articles, allow_destroy: true end
spec/factories/article.rb
FactoryBot.define do factory :article do title { '記事タイトル' } content { '記事の文章' } publish_time {'2019/3/3'} status {'publish'} slug {'aa'} category_id {1} after(:create) do |article| article.categories << create(:category) end end end
spec/model/article_spec.rb
require 'rails_helper' describe Article, type: :model do describe do it 'contentがnil' do user = FactoryBot.create(:article, content:nil) expect(user).not_to be_valid end end end
エラーメッセージ
2) Article contentがnil Failure/Error: user = FactoryBot.create(:article, content:nil) ActiveRecord::RecordInvalid: バリデーションに失敗しました: Category ids translation missing: ja.activerecord.errors.models.article.attributes.category_ids.blank # ./spec/model/article_spec.rb:7:in `block (3 levels) in <top (required)>'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/22 08:28
2020/01/22 08:30
2020/01/22 10:43