gpt4 book ai didi

ruby-on-rails - 工厂女孩争论错误 : Factory not registered:

转载 作者:数据小太阳 更新时间:2023-10-29 07:19:44 25 4
gpt4 key购买 nike

我已经完成了看似所有正确的步骤,但我仍然收到此错误。来自 gem 文件:

 group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'database_cleaner'
gem 'factory_girl_rails', '~> 4.0', require: false
gem 'faker'
end

spec_helper.rb:

require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
FactoryGirl.factories.clear
end

规范/工厂/公司.rb: 需要'spec_helper' 需要'伪造者'

FactoryGirl.define do
factory :company do
name { Faker::Name.name }
system_name { Faker::Company.name }
domain { Faker::Internet.url }
end
end

公司规范.rb: 需要'spec_helper'

describe 'Company' do
it 'has a valid factory' do
FactoryGirl.build(:company).should be_valid
end
end

获取错误失败/错误:

Failure/Error: FactoryGirl.create(:company).should be_valid
ArgumentError:
Factory not registered: company

最佳答案

我把所有的东西都放错了地方:

gemfile 需要:

group :development, :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'database_cleaner'
gem 'factory_girl_rails', '~> 4.0', require: false
gem 'faker'
end

我将 spec_helper 中的所有内容移至 rails_helper.rb 并去掉了两行:

require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
// FactoryGirl.find_definitions // got rid of this
// FactoryGirl.factories.clear // got rid of this
end

公司规范.rb:

require 'rails_helper'

describe 'Company' do
it 'has a valid factory' do
expect(build(:company)).to be_valid
end
end

大部分信息来自评论并源自此 tutorial关于 rspec 和工厂女孩。

关于ruby-on-rails - 工厂女孩争论错误 : Factory not registered:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com