所以我有一个奇怪的问题,我宣布了3个工厂,其中只有1个工厂处于初始化状态,其余部分正在给予Unintialized常数。 我不知道我做错了什么。 我知道这个问题被问了很多次,但没有一个答案能解决我的问题。
这是我的spec / factories / sample_factory.rb
require 'faker'
FactoryGirl.define do
factory :early_access do
email {Faker::Internet.email}
end
factory :login do
email {Faker::Internet.email}
password "password"
end
factory :user do
display_name {Faker::Internet.name}
email {Faker::Internet.email}
username {Faker::Internet.username}
password "password"
end
这是型号规格文件spec / models / sample.rb
require 'spec_helper.rb'
describe EarlyAccess do
it "has a valid factory" do
FactoryGirl.build(:early_access).should be_valid
end
it "has a valid factory login" do
FactoryGirl.build(:login).should be_valid
end
end
it "has valid factory user" do
FactoryGirl.build(:user).should be_valid
end
end
其中,只有前一个early_access
通过,其余的通过
.FF
Failures:
1) EarlyAccess has a valid factory 2
Failure/Error: FactoryGirl.build(:login).should be_valid
NameError:
uninitialized constant Login
# ./spec/models/sample.rb:10:in `block (2 levels) in <top (required)>'
2) EarlyAccess has valid factory user
Failure/Error:
NameError:
uninitialized constant User
# ./spec/models/sample.rb:16:in `block (2 levels) in <top (required)>'
我在spec_helper中包含了factory_girl_rails。
答案 0 :(得分:2)
如果您将这些模型放在某个非标准目录中 - 手动要求它们或将目录添加到config / application.rb config.autoload_paths数组。