1

我有两个工厂——用户和公司。

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> ...

我很困惑。有任何想法吗?

4

1 回答 1

2

你能告诉我用户的初始化方法吗?我认为出现此问题是因为您未在用户或公司工厂中定义某些关联

于 2012-04-17T01:09:52.867 回答