Ruby-on-rails 工厂未注册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17330788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:14:08  来源:igfitidea点击:

Factory not registered

ruby-on-railsrubyrspec

提问by Kert

I want to test a model with RSpec but I probably have stumbled on a typo that I just can't find. Can somebody please help me a bit? I've been struggling with it for a long time and just can't find any mistakes. Thank you in advance!

我想用 RSpec 测试一个模型,但我可能偶然发现了一个我找不到的错字。有人可以帮我一下吗?我已经为此苦苦挣扎了很长时间,但找不到任何错误。先感谢您!

user_spec.rb

用户规范.rb

require 'spec_helper'

describe User do
     it "has a valid factory" do
        FactoryGirl.build(:user).should be_valid
     end
     it "is invalid without an e-mail"
     it "is invalid without a correct e-mail" 
     it "is invalid without a password" 
     it "is invalid without a matching password confrimation" 
end

user.rb

用户名

FactoryGirl.define do
    factory :user do |f|
        f.email "[email protected]"
        f.password "ruby"
        f.password_confrimation "ruby"
    end
end

spec_helper.rb

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

error

错误

Factory not registered: user

回答by fontno

You have your factory definition in the wrong file, according to your question it is in user.rb. This needs to be in a factories.rb in your test folder (spec) if you use rspec

您的工厂定义在错误的文件中,根据您的问题,它在 user.rb 中。如果您使用 rspec,这需要在您的测试文件夹(spec)中的 factory.rb 中

# user.rb

FactoryGirl.define do
    factory :user do |f|
        f.email "[email protected]"
        f.password "ruby"
        f.password_confrimation "ruby"
    end
end

Change above to this, (Also you don't need the f variable)

将上面更改为此,(您也不需要 f 变量)

# spec/factories.rb

FactoryGirl.define do
    factory :user do
        email "[email protected]"
        password "ruby"
        password_confrimation "ruby"
    end
end

Also as the comments say, make sure gem 'factory_girl_rails'is in your Gemfile, instead of just gem 'factory_girl'

同样正如评论所说,确保gem 'factory_girl_rails'在你的Gemfile, 而不仅仅是gem 'factory_girl'

回答by zhisme

I had run into the following issue with the structure in my folder factories/ - factories/ -- artists.rb -- techniques.rb

我的文件夹工厂中的结构遇到了以下问题/ - factories/ -- artists.rb -- techniques.rb

artists.rb

artists.rb

FactoryBot.define do
    factory :artist do
      name       'Michael'
      technique   FactoryBot.create :technique
    end
  end

techniques.rb

techniques.rb

FactoryBot.define do
  factory :technique do
    name 'Some name'
  end
end

So it was loading artist before technique object were loaded. So it couldn't find it. The solution is to not use nested FactoryBot create in factories or rename your nested factories to something that stands before your parent factory.

所以它是在加载技术对象之前加载艺术家。所以它找不到它。解决方案是不要在工厂中使用嵌套的 FactoryBot create 或将嵌套的工厂重命名为位于父工厂之前的名称。

I just moved my technique factory. to factories.rband defined it there. And issue was resolved.

我刚搬了我的技术工厂。到factories.rb并在那里定义它。并解决了问题。

回答by Kyle Poretto

I had the same issue and the only thing I added was a factories.rb file. All tests passed from that.

我遇到了同样的问题,我添加的唯一内容是 factory.rb 文件。所有测试都通过了。

回答by slhck

I got this error when running the Rails application in the production environment.

在生产环境中运行 Rails 应用程序时出现此错误。

Usually, the Gem factory_bot_railsis only added here:

通常,Gemfactory_bot_rails只添加在这里:

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

You should add these to the "general" list of Gems if you want to use them in production.

如果您想在生产中使用它们,您应该将它们添加到 Gems 的“常规”列表中。