2011-09-16 2 views
1

Ruby on Rails 3.0.10, RSpec 2 및 FactoryGirl을 사용하고 있습니다. 나는 다음과 같은 시나리오가 : 나는 user.rb 파일에서FactoryGirl 객체를 찾는 데 문제가 있습니다.

FactoryGirl.define do 
    factory :user, :class => User do |user| 
    user.authorization 'registered' 
    end 
end 

이 나는 ​​factories/user.rb 파일에서

describe User do 
    let(:user) { Factory(:user) } 

    it "should have a 'registered' authorization do 
    user.authorization.should == "registered" 
    end 
end 

이있는 models/user_spec.rb 파일에서

을 나는이 :

class User < ActiveRecord::Base 
    DEFAULT_AUTHORIZATION = 'registered' 

    validates :authorization, 
    :inclusion => { 
     :in  => Authorization.all.map(&:name), 
     :message => "authorization is not allowed" 
    }, 
    :presence => true 

    before_validation :fill_user_create, :on => :create 


    private 

    def fill_user_create 
    self.authorization = Authorization::DEFAULT_AUTHORIZATION 
    end 
end 

를 실행할 때나는 다음과 같은 오류 get 명령 : 정확히 문제가 무엇

User should have a default 'registered' Authorization 
Failure/Error: let(:user) { Factory(:user) } 
ActiveRecord::RecordInvalid: 
    Validation failed: Users authorization is not allowed 

을하고 나는 그것을 어떻게 해결할 수 있습니까?


은 BTW 다음 models/user_spec.rb 파일에서 나는 다음과 같은

let(:user) { User.create } 

같은 것을 사용할 수 있으며, 그것은 작동하지만 은 내가 FactoryGirl 보석를 사용하는 것을 선호합니다. 당신은 무엇에 관해 조언합니까?

+1

'self.authorization = 인증 :: DEFAULT_AUTHORIZATION' – eugen

+0

@eugen - 나는 질문을 정정했다. – Backo

답변

0

은 아래로 사양을 수정 시도하고 결과가 무엇인지 확인할 수 있습니다 :

당신은 아마 할 의미했다 fill_user_create 방법에
it "should have a 'registered' authorization" do 
    system_names = Authorization.all.map(&:system_name) 
    system_names.should have_at_least(1).item 
    system_names.should include('registered') 
    user.authorization.should == "registered" 
end 
+0

자세한 내용은 http://stackoverflow.com/questions/7457403/trouble-on-evaluating-other-model-data를 참조하십시오. – Backo

관련 문제