2012-05-18 5 views
1

У меня есть User has_many :accounts, through: :roles и User has_many :owned_accounts, through: :ownerships, я использую STI, где Ownership < Roles. Я не могу написать рабочий завод для модели Ownership и ассоциации owned_account, и мои тесты не работают.Создание ассоциаций с FactoryGirl

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 

У меня есть рабочие заводы для пользователя, учетной записи и роли; Я не могу писать завод по собственности и ассоциация owned_accounts, однако:

FactoryGirl.define do 
    factory :user do 
    name "Elmer J. Fudd" 
    end 
    factory :account do 
    name "ACME Corporation" 
    end 
    factory :owned_account do 
    name "ACME Corporation" 
    end 
    factory :role do 
    user 
    account 
    end 
    factory :ownership do 
    user 
    owned_account 
    end 
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)>' 

ответ

3

В сообщение об ошибке связано с тем, что вам нужно указать родительский элемент, или предполагается, что заводское определение относится к классу ActiveRecord этого имени.

factory :owned_account, :parent => :account do 
    name "ACME Corporation" 
end 
Смежные вопросы