2013-04-16 5 views
1

모델 필드의 유효성을 테스트하기 전에 주제에 인스턴스 변수를 설정하려고합니다. 유효성 검사는 조건부 (일부 사용자 유형에만 사용됨)이기 때문에이 변수를 설정해야합니다.Shoulda 및 RSpec 전

context "as a user" do 

    before(:each) do 
     subject = Organization.new 
     subject.editor = "user" 
    end 

    it { subject.should validate_presence_of :name } 

    end 

그러나 예상대로 작동하지 않습니다 :

Failure/Error: it { subject.should validate_presence_of :description } 
RuntimeError: 
    Organization#editor attr is not set 

내가 무엇을 그리워 않았다 그래서 나는 이런 일이?

답변

3

이전 블록의 subject은 로컬 변수입니다. 너는 an explicit subject을 사용하려는 것 같습니다 :

context "as a user" do 
    subject { Organization.new } 

    before(:each) do 
    subject.editor = "user" 
    end 

    # usually, you don't explicitly name the subject in an `it` like this 
    it { should validate_presence_of :name } 

end