2011-10-01 6 views
0

Ruby on Rails 3.1.0, rspec-rails 2Factory 보석을 사용하고 있습니다. Account 클래스의 Factory 객체를 선언 할 때 유효성 검사 프로세스와 관련된 몇 가지 문제가 있습니다. 모델 파일에서 팩토리 객체 초기화시 문제가 발생했습니다

내가 가진 : 공장 출하시 파일에서
class Account < ActiveRecord::Base 
    belongs_to :user 

    attr_accessible :name, ..., :password 

    validates :name, 
    :presence => true 

    ... 

    validates :password, 
    :presence => true 
end 

내가 가진 :

FactoryGirl.define do 
    factory :account, do 
    sequence(:name) { |n| "Foo #{n}"} 
    ... 
    password 'psw_secret' 
    association :user 
    end 

    factory :user do 
    auth 'registered' 
    end 
end 

사양 파일에 나는 예상대로 작동 let!(:account) { Factory(:account) }을 명시하지만 사용할 때 다음과 같은 경우 :

let!(:user) { Factory(:user, :account => Factory(:account)) } 

는이 오류를 얻을 :

,536,
Failure/Error: let!(:user) { Factory(:user, :account => Factory(:account)) } 
ActiveRecord::RecordInvalid: 
    Validation failed: Account password can not be blank, Account is invalid 

그 오류가 발생합니까? 문제를 어떻게 해결할 수 있습니까?

+0

': user' 팩토리로 업데이트 할 수 있습니까? – nowk

+0

@kwon - 질문을 업데이트했습니다. – user12882

답변

1

난 당신이 다른 방식으로 수행해야한다고 생각 :

@user = Factory(:user) 
@account = Factory(:account, :user => @user) 

관계가 없습니다 useraccount에 정의된다. 희망이 도움이됩니다.

관련 문제