所以我有一个奇怪的问题,我声明了 3 个工厂,其中只有 1 个工厂初始化正常,其余的都给未初始化常量。我不知道我做错了什么。我知道这个问题已经被问过很多次了,但没有一个答案能解决我的问题。
这是我的规范/工厂/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。