我有两个工厂——用户和公司。
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}@exyzample.com" }
password "secret"
password_confirmation "secret"
end
factory :customer, :parent => :user do
role "customer"
end
factory :company do
name "User Company"
association :user, :factory => :customer
end
end
然后,当我从工厂创建公司时 - 一切都很完美,记录保存到数据库并与用户关联。很酷,但是当我尝试测试用户能力时 - 我的所有测试都失败了。
有一个例子(rails 控制台):
company = Factory(:company)
=> COMMIT
Ability.new(company.user)
=> Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
但
company.user
=> #<User id: 1, email: "user1@example.com", ..., role: "customer">
和
Ability.new(User.find(1))
=> #<Ability:0xcf6771c> ...
我很困惑。有任何想法吗?