I have an app for which I have made a large number...
# support
c
I have an app for which I have made a large number of customizations, mainly overrides and deface, but I didn’t take the time to write tests—primarily because I’m not that familiar with rspec. I am finally taking the time to do this, and I’ve run into an error with trying to create a product in rspec. I’ll include the full context as part of the thread so as to not clutter up the main support window.
Here’s the context followed by the error: overrides/spree/models/product/spree_product_mobility_i18n.rb
Copy code
module Spree
  class Product < Spree::Base
    module ProductMobilityI18n
      def self.prepended(base)

        base.extend Mobility
        base.has_rich_text :description # Original description, replacing with i18n_description
        base.translates :i18n_name, type: :string
        base.translates :i18n_description, backend: :action_text
        base.before_save :set_description_and_name

      end

      def set_description_and_name
        # insure original column is filled with the default locale
        self.description = i18n_description(locale: I18n.locale)
        self.name = i18n_name(locale: I18n.locale)
      end

      Spree::Product.prepend self unless Spree::Product.included_modules.include?(self)
    end
  end
end
config/application.rb
Copy code
#...
    # Factory Bot for Rspec
    # Don't initialize FactoryBot if it's not in the current Bundler group.
    if defined?(FactoryBotRails)
      initializer after: 'factory_bot.set_factory_paths' do
        require 'spree/testing_support/factory_bot'

        # The paths for Solidus factories.
        solidus_paths = Spree::TestingSupport::FactoryBot.definition_file_paths

        # Optional: Any factories you want to require from extensions.
        extension_paths = [
          MySolidusExtension::Engine.root.join("lib/solidus_content/factories/order.rb"),
          MySolidusExtension::Engine.root.join("lib/solidus_content/factories/product.rb"),
        ]

        # Your application's own factories.
        app_paths = [
          # Rails.root.join('lib/factories'),
          Rails.root.join('spec/factories'),
        ]

        FactoryBot.definition_file_paths = solidus_paths + extension_paths + app_paths
      end
    end
    #...
spec/system/login_spec.rb
Copy code
RSpec.describe 'The product admin' do
  stub_authorization!

  it 'allows me to view the products' do
    product = create(:product, name: 'Test Product')
    product.with_locale(:en) { product.name = 'Test Product' }
    product.with_locale(:ja) { product.name = 'テスト商品' }
    product.save!

    puts "Product name: #{product.name}"

    visit spree.admin_path
    click_link 'Products'

    expect(page).to have_content(product.name)
  end
end
spec/rails_helper.rb
Copy code
# …
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
# …
spec/support/solidus.rb
Copy code
require 'spree/testing_support/authorization_helpers'
require 'spree/testing_support/order_walkthrough'
require 'spree/testing_support/capybara_ext'
require 'spree/testing_support/preferences'
spec/support/factory_bot.rb
Copy code
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
Error:
Copy code
Failures:

  1) The product admin allows me to view the products
     Failure/Error: puts create(:product, name: 'Test Product', price: 12.99, sku: 'TEST-001')
     
     ActiveRecord::NotNullViolation:
       PG::NotNullViolation: ERROR:  null value in column "name" of relation "spree_products" violates not-null constraint
       DETAIL:  Failing row contains (7, null, null, 2022-06-06 00:33:04.22143, null, test-product, null, null, 7, 7, 2023-06-06 00:33:04.510008, 2023-06-06 00:33:04.510008, t, null, null, null, null, null, null).
     # ./spec/system/login_spec.rb:7:in `block (2 levels) in <main>'
     # ------------------
     # --- Caused by: ---
     # PG::NotNullViolation:
     #   ERROR:  null value in column "name" of relation "spree_products" violates not-null constraint
     #   DETAIL:  Failing row contains (7, null, null, 2022-06-06 00:33:04.22143, null, test-product, null, null, 7, 7, 2023-06-06 00:33:04.510008, 2023-06-06 00:33:04.510008, t, null, null, null, null, null, null).
     #   ./spec/system/login_spec.rb:7:in `block (2 levels) in <main>'
I’l write a better test once I can figure out how to create a product successfully. While I’m waiting for a response I’ll consult how it’s being created by Factory Bot and do some research on how Solidus implements Factory Bot. Thank you in advance.
Alright, I think I sorted it out. I had to learn how to make my own factory for this new product model.
Copy code
FactoryBot.define do
  factory :mobility_friendly_product, class: Spree::Product do
    sequence(:i18n_name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" }
    i18n_description { "As seen on TV!" }
    price { 19.99 }
    cost_price { 17.00 }
    sku { generate(:sku) }
    available_on { 1.year.ago }
    deleted_at { nil }
    shipping_category do |r|
      Spree::ShippingCategory.first ||
        r.association(:shipping_category, strategy: :create)
    end
  end
end