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

问题描述 投票:0回答:3

对于我的第一个 Ruby on Rails 引擎 - 名为 “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
创建了以下文件:

  • 应用程序/模型/词汇表/user.rb
  • db/migrate/20180731061831_create_glossary_users.rb
  • 规范/模型/词汇表/user_spec.rb
  • 规范/工厂/glossary_users.rb

看起来不错。

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 控制台中发出以下命令不会返回预期的路径,并且仍然找不到工厂:

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>  

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

感谢您的帮助!

ruby-on-rails factory-bot rspec-rails
3个回答
1
投票

我遇到了类似的问题,我将代码重新排列为

# rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# for factory bot
require 'factory_bot_rails'

我的 test.rb 为

# test.rb
config.eager_load = false
# some config
config.factory_bot.definition_file_paths += ["<my path"]

编辑1:解释 因此 FactoryBot 扫描

FactoryBot.definition_file_paths
中存在的目录并通过调用
FactoryBot.find_definitions

注册所有工厂

FactoryBot.find_definitions
调用发生在我们

require 'factory_bot_rails`

因此

rails_helper.rb
内的语句顺序很重要

编辑2:上述解决方案忽略了其他人指出的工厂使用中的拼写错误


0
投票

工厂名称为

glossary_user
,应使用为
FactoryBot.build(:glossary_user)


0
投票

在你的

lib/my_engine/engine.rb

module MyEngine
  class Engine < ::Rails::Engine

    initializer :load_factories, after: 'factory_bot.set_factory_paths' do
      if defined?(FactoryBot) && defined?(Faker) && !Rails.env.production?
        FactoryBot.definition_file_paths.prepend(
          File.join(MyEngine.root, 'spec', 'factories')
        )
      end
    end
  end
end

我喜欢用条件来保护它,以确保它只在适当的条件下运行。您可以在 FactoryBot 代码库中找到与此相关的其他变体。

© www.soinside.com 2019 - 2024. All rights reserved.