ruby-on-rails - 我在使用 RSpec 和 Factory-bot-rails 测试 Controller 时得到 'KeyError: Factory not registered: "user"'

标签 ruby-on-rails postgresql rspec factory-bot

我在 ToDo 应用程序中工作,当我使用 FactoryBot 运行 Controller 测试时出现错误。

该应用程序使用 Ruby on Rails,使用 Postgres 作为数据库,使用 RSpec 和 Shoulda-Matchers 进行测试,使用 FactoryBot-Rails。

Controller 有一个用户之前登录的验证器,需要加载用户列表。为此,我使用 FactoryBot 来模拟用户。

当我运行我的列表 Controller 的测试时,我收到错误“KeyError: Factory not registered: "user"”

我在 StackOverflow 和其他页面上阅读了很多博客,并且尝试了所有找到的解决方案,包括:

  • 我确保在我的 Gemfile 中使用“factory_bot_rails”
  • 尝试在我的 spec 文件夹中创建一个名为“factories”的文件
  • 在“spec/factories/user.rb”中尝试一个名为 user.rb 的文件
  • 尝试使用“factory_girl_rails”(我知道现在是 FactoryBot,但我试过了)
  • 使用和不使用 shoulda-matchers 更改测试
  • 更改测试类型
  • 同时进行多项或仅一项测试
  • 在我的 spec_helper.rb 文件中需要“factory_bot_rails”
  • 在我的测试文件中需要我的文件或工厂文件夹
  • 在我的 rails_helper 文件中包含“Syntax::Methods”

在“spec/factories.rb”中定义“用户”工厂

FactoryBot.define do 
  factory :user do
    first_name { "User1" }
    last_name { "UserLastName" }
    description { "A big guy" }
    email { "test@example.com" }
    password { "012345" }
  end
end

要在“lists_controller.rb”中呈现的索引

before_action :authenticate_user!
def index
  @lists = current_user.lists
end

定义用于测试的用户,并测试 'lists_controllers_spec.rb' 中索引的 render_template

require 'rails_helper'
require 'factories/user'

RSpec.describe ListsController, type: :controller do

  let(:user) { create(:user) }
  let(:list) { create(:list, user: user) }

  before do 
    sign_in user 
  end

  describe 'Get #index' do
    before { get :index }
    it { should render_template(:index) }
  end
end

在我定义的Gemfile中

group :development, :test do
  gem 'factory_bot_rails'
end

rails_helper.rb

require 'spec_helper'

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production 
abort("The Rails environment is running in production mode!") if ails.env.production?

require 'rspec/rails'

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true

  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!

  config.include Devise::Test::ControllerHelpers, type: :controller 
  config.include Devise::Test::IntegrationHelpers, type: :request 

  Shoulda::Matchers.configure do |config|
    config.integrate do |with|
      with.test_framework :rspec
      with.library :rails
    end
  end
end

我希望测试显示索引渲染正常,但我总是得到

Failures:

  1) ListsController Get #index 
    Failure/Error: let(:user) { create(:user) }

    KeyError:
      Factory not registered: "user"
    # ./spec/controllers/lists_controller_spec.rb:14:in `block (2 levels) in <top (required)>'
    # ./spec/controllers/lists_controller_spec.rb:18:in `block (2 levels) in <top (required)>'
    # ------------------
    # --- Caused by: ---
    # KeyError:
    #   key not found: "user"
    #   ./spec/controllers/lists_controller_spec.rb:14:in `block (2 levels) in <top (required)>'

Finished in 0.00451 seconds (files took 1.04 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/lists_controller_spec.rb:30 # ListsController Get #index

spec_helper.rb

require 'factory_bot_rails'
RSpec.configure do |config|

  config.before(:all) do
    FactoryBot.reload
  end

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
end

最佳答案

键错误: 工厂未注册:“文章”

当我尝试通过命令 FactoryBot.factories 检查工厂时 结果我 这意味着由于配置中的自定义设置,工厂没有得到注册

您可以通过将以下内容添加到 config/application.rb 或 config/environments 中的适当环境配置来进行配置:

config.factory_bot.definition_file_paths = ["custom/factories"]

我删除了它,它开始为我工作:)

关于ruby-on-rails - 我在使用 RSpec 和 Factory-bot-rails 测试 Controller 时得到 'KeyError: Factory not registered: "user"',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994679/

相关文章:

ruby-on-rails - Rails 在 url 中使用 '.' 截断参数

php - 警告 : pg_query(): Query failed: ERROR: syntax error at or near

ruby-on-rails - 从我的应用程序代码中获取数据并进入我的 rspec 测试

Ruby 元编程,RSpec 的 'should' 是如何工作的?

php - 从 PHP 清理 PostgreSQL 数据库

ruby - 如何以 rspec 方式改进此 RSpec 代码?

ruby-on-rails - 重定向到 Rails 3 中的子域

ruby-on-rails - jruby/activerecord-jdbc/tomcat/DB2 准备好用于企业了吗?

ruby-on-rails - Rails 将 HTML 作为 JSON 传递,然后渲染

sql - 如何通过过滤器有效地获得最小值和最大值?