1
我有兩個工廠 - 用戶和公司。CanCan + Factory_Girl - 奇怪的行爲
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
然後,當我從工廠創建公司 - 一切都是完美的,記錄保存到數據庫和相關的用戶。酷,但當我試圖測試用戶能力 - 我所有的測試都落空了。
有一個例子(軌控制檯):
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: "[email protected]", ..., role: "customer">
而且
Ability.new(User.find(1))
=> #<Ability:0xcf6771c> ...
我很困惑。有任何想法嗎?
你是完全正確的。我忘了把我的公司與產品聯繫起來。謝謝! – user1337413 2012-04-17 01:13:24