Cody Baldwin
06/06/2023, 12:41 AMCody Baldwin
06/06/2023, 12:46 AMmodule 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
#...
    # 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
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
# …
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
# …
spec/support/solidus.rb
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
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
Error:
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.Cody Baldwin
06/06/2023, 1:42 AMFactoryBot.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