rspec + заводская модель тестирования вложенных атрибутов

У меня есть приложение rails4 с rspec + factory_girl. Я хотел бы проверить валидацию, которая гарантирует, что продукт имеет хотя бы одну функцию, конкуренцию, сценарий использования и отрасль. Первые 3 должны принадлежать продукту, но промышленность может существовать сама по себе. Я еще не пытался поставить индустрию в тест, так как я не могу даже сделать работу первые 3.

Я попробовал подход ниже, в котором я создаю фабрику продуктов, которая имеет product_feature, product_competition и product_usecase. По какой-то причине это не работает.

Я использую правильный подход? Если так, что не так с моим кодом?

1) Product nested attribute validation has a valid factory
     Failure/Error: expect(create(:product_with_nested_attrs)).to be_valid

     ActiveRecord::RecordInvalid:
       Validation failed: You have to choose at least 1 industry., You must have at least 1 product feature., You must name at least 1 competition., You must describe at least 1 usecase.

product.rb (ОБНОВЛЕНО)

belongs_to :user
has_many :industry_products, dependent: :destroy, inverse_of: :product
has_many :industries, through: :industry_products #industry exists without product; connected with has_many thru association
has_many :product_features, dependent: :destroy
has_many :product_competitions, dependent: :destroy
has_many :product_usecases, dependent: :destroy

accepts_nested_attributes_for :industry_products, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :product_features, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :product_competitions, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :product_usecases, reject_if: :all_blank, allow_destroy: true

#UPDATE

validate :product_features_limit #Also have it for product_usecases and product_competititons

def product_features_limit
  if self.product_features.reject(&:marked_for_destruction?).count > 10
    self.errors.add :base, "You can't have more than 10 features."
  elsif self.product_features.reject(&:marked_for_destruction?).count < 1
    self.errors.add :base, "You must have at least 1 product feature."
  end
end

завод

FactoryGirl.define do

  factory :product_competititon do
    competitor { Faker::Commerce.product_name }
    differentiator { Faker::Lorem.paragraph }
    product
  end

  factory :product_feature do
    feature { Faker::Lorem.paragraph }
    product
  end

  factory :product_usecase do
    example { Faker::Lorem.sentence }
    detail { Fakert::Lorem.paragraph }
    product
  end

  factory :product do
    name { Faker::Commerce.product_name }
    company { Faker::Company.name }
    website { 'https://example.com' }
    oneliner { Faker::Lorem.sentence }
    description { Faker::Lorem.paragraph }
    user

    factory :product_with_nested_attrs do
      transient do
        nested_attrs_count 1
      end
      after(:create) do |product, evaluator|
        create_list(:product_feature, evaluator.nested_attrs_count, product: product)
        create_list(:product_competititon, evaluator.nested_attrs_count, product: product)
        create_list(:product_usecase, evaluator.nested_attrs_count, product: product)
      end
    end
  end
end

product_spec.rb

RSpec.describe Product, type: :model do

  describe "nested attribute validation" do

    it "has a valid factory" do
      expect(create(:product_with_nested_attrs).to be_valid
    end

  end
end

Ответы на вопрос(1)

Ваш ответ на вопрос