如何在 Rails 引擎中配置 FactoryBot 文件路径?

How to configure FactoryBot files path in a Rails engine?

对于我在 Rails 引擎上的第一个 Ruby - 名为 "glossary" - 我希望实现 Rspec,ShouldaMatchers, FactoryBot 作为测试套件。事情看起来不错,但 FactoryBot 一直声称工厂未注册:

Failures:

  1) Glossary::User   
     Failure/Error: subject {FactoryBot.build(:user)}

     ArgumentError:
       Factory not registered: user 

在开发过程中, rails generate model user first_name last_name user_name 创建了以下文件:

看起来不错。

rails_helper.rb 包含以下内容:

    ENV['RAILS_ENV'] ||= 'test'
    require File.expand_path('../dummy/config/environment.rb', __FILE__)

    require 'spec_helper'

    # Prevent database truncation if the environment is production
    abort("The Rails environment is running in production mode!") if Rails.env.production?
    require 'rspec/rails'
    require 'shoulda/matchers'
    require 'factory_bot_rails'
    FactoryBot.definition_file_paths << File.expand_path('../spec/factories', __FILE__)

---

    # Fred 2018-07-29: added to engine configuration
    Shoulda::Matchers.configure do |config|
      config.integrate do |with|
        # Choose a test framework:
        with.test_framework :rspec

        # Choose one or more libraries:
        with.library :rails
      end
    end

    # spec/support/factory_bot.rb
    RSpec.configure do |config|
      config.include FactoryBot::Syntax::Methods
    end

lib/glossary/engine.rb 包含以下内容:

module Glossary
  class Engine < ::Rails::Engine
    isolate_namespace Glossary

    # Fred 2018-07-29: added to engine configuration
    config.generators do |g|
      g.test_framework :rspec
      g.fixture_replacement :factory_bot 
      g.factory_bot dir: 'spec/factories' 
    end
  end
end

FactoryBot 生成了这个 spec/factories/glossary_users.rb 文件:

FactoryBot.define do
  factory :glossary_user, class: 'Glossary::User' do
    first_name "MyString"
    last_name "MyString"
    user_name "MyString"
  end
end

我在 spec/models/glossary/user_spec.rb 文件中引用:

require 'rails_helper'

module Glossary
  RSpec.describe User, type: :model do
    describe 'Validations'
    subject {FactoryBot.build(:user)}
      it { should validate_presence_of(:user_name) }

  end
end

然而,在 rails 控制台中发出以下命令并没有 return 预期的路径,并且仍然找不到工厂:

Fred:glossary$ rails console                                                                                                                 
Loading development environment (Rails 5.2.0)                                                                                                                   
irb(main):001:0> FactoryBot.find_definitions                                                                                                                    
=> ["/var/www/glossary/spec/dummy/factories", "/var/www/glossary/spec/dummy/test/factories", "/var/www/glossary/spec/dummy/spec/factories"]                     
irb(main):002:0>  

我是否遗漏了设置或配置中的某些内容?

感谢您的帮助!

工厂名称为 glossary_user,应用作 FactoryBot.build(:glossary_user)