Ruby on rails 创建与FactoryGirl的关联

Ruby on rails 创建与FactoryGirl的关联,ruby-on-rails,rspec,factory-bot,Ruby On Rails,Rspec,Factory Bot,我有一个用户有许多:帐户,通过::角色和一个用户有许多:拥有的帐户,通过::所有者,我在使用STI,其中所有权

我有一个
用户有许多:帐户,通过::角色
和一个
用户有许多:拥有的帐户,通过::所有者
,我在使用STI,其中
所有权
。我无法为
所有权
模型和
自有账户
关联编写工作工厂,我的测试失败

class User < ActiveRecord::Base
  has_many :roles
  has_many :accounts, through: :roles
  has_many :ownerships
  has_many :owned_accounts, through: :ownerships
end
class Account < ActiveRecord::Base
  has_many :roles
  has_many :users, through: :roles
end
class Role < ActiveRecord::Base
  belongs_to :users
  belongs_to :accounts
end
class Ownership < Role
end
我从这些测试开始,但我得到一个未初始化的常量错误,所有测试都失败了:

describe Ownership do
  let(:user)    { FactoryGirl.create(:user) }
  let(:account) { FactoryGirl.create(:owned_account) }
  before do
    @ownership = user.ownerships.build
    @ownership.account_id = account.id
  end
  subject { @ownership }
  it { should respond_to(:user_id) }
  it { should respond_to(:account_id) }
  it { should respond_to(:type) }
end

1) Ownership 
     Failure/Error: let(:account) { FactoryGirl.create(:owned_account) }
     NameError:
       uninitialized constant OwnedAccount
     # ./spec/models/ownership_spec.rb:17:in `block (2 levels) in <top (required)>'
     # ./spec/models/ownership_spec.rb:21:in `block (2 levels) in <top (required)>'
描述所有权
let(:user){FactoryGirl.create(:user)}
let(:account){FactoryGirl.create(:owned_account)}
在做之前
@所有权=user.ownerships.build
@ownership.account\u id=account.id
结束
主体{@ownership}
它{应该响应(:user_id)}
它{应该响应(:account\u id)}
它{应该响应(:type)}
结束
1) 所有权
失败/错误:let(:account){FactoryGirl.create(:owned_account)}
名称错误:
未初始化常量OwnedAccount
#./spec/models/ownership_spec.rb:17:in'block(2层)in'
#./spec/models/ownership_spec.rb:21:in'block(2层)in'

错误消息是因为您需要指定父级,否则它将假定工厂定义是针对该名称的ActiveRecord类的

factory :owned_account, :parent => :account do
  name "ACME Corporation"
end
factory :owned_account, :parent => :account do
  name "ACME Corporation"
end