【问题标题】:Cannot initialize factory bot factories in Rails Engine无法在 Rails Engine 中初始化工厂机器人工厂
【发布时间】:2021-01-19 13:41:49
【问题描述】:

我有一个 Rails 5.2 引擎,我需要在它上面运行一些系统(带有 capybara)测试。 我也在为此使用工厂,使用 factory_bot_rails gem。

要运行测试,我需要使用命令rails test:app:system,因为标准rails test:system 会出错,因为它是一个引擎。 所以系统测试是在虚拟应用的上下文中运行的。

在启动之前它给出了Factory xxxx not initialized的错误,我想是因为所有工厂都在test/factories,而虚拟应用程序在test/dummy并且无法加载它们。

如何为引擎运行我的系统测试? 我认为我的选择是在引擎上下文中运行系统测试,或者将工厂加载到虚拟...

【问题讨论】:

    标签: ruby-on-rails integration-testing factory-bot rails-engines


    【解决方案1】:

    是的,我相信你是对的,你需要在引擎上下文中运行测试套件。

    我认为您需要:

    # rails_helper.rb or spec_helper.rb
    
    require 'factory_bot_rails'
    

    如果事情仍然不稳定,应该能够添加确切的目录:

    require 'factory_bot_rails'
    
    FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
    FactoryBot.find_definitions
    

    这里解释https://www.hocnest.com/blog/testing-an-engine-with-rspec/

    另外,请确保您已将 config.include FactoryBot::Syntax::Methods 包含在您的 rails_helper.rb

    【讨论】:

      猜你喜欢
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多