简体   繁体   中英

RSpec factories with FactoryBot 'uninitialized constant FactoryBot'

I am setting up request specs following this tutorial . If I run the tests with bundle exec rspec I get the following error:

An error occurred while loading ./spec/factories/entities_spec.rb.
Failure/Error:
  FactoryBot.define do
    factory :entity do
      name { FFaker::Lorem.word }
    end
  end

NameError:
  uninitialized constant FactoryBot

The tutorial only asks to add some configuration to the rails_helper.rb file. I can't figure where I need to add a require 'factory_bot' . If I add it to the top of the rails_helper.rb file, the error persists. And if I add it to the factories/entity_spec.rb file, I get this error:

An error occurred while loading ./spec/requests/entities_spec.rb.
Failure/Error:
  factory :entity do
    name { FFaker::Lorem.word }
  end

FactoryBot::DuplicateDefinitionError:
  Factory already registered: entity

只需将我的factories/*.rb文件从例如factories/user_spec.rb重命名为factories/user.rb解决此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM