2012-02-17 7 views
0

나는 레일 3.2 응용 프로그램에 대한 몇 가지 RSpec에 테스트를 쓰고 있어요, 그리고 RSpec에이 테스트에 충돌 계속 실행 : 나는 RSpec을 실행하면레일 3.2 - RSpec에 - 정의되지 않은 방법 오류 동안 RSpec에 테스트

describe Person do 
    before { @person = Person.new(names: [Factory.create(:name)], DOB: Date.today) } 
    subject { @person } 

    [:names,:DOB,:phone,:email,:address1,:address2, 
    :city,:state,:zip,:notes,:last_seen].each do |value| 
    it { should respond_to(value) } 
    end 

    it { should be_valid } 

    describe "when no name is present" do 
    @person.names = [] 
    it {should be_invalid} 
    end 

    describe "when no DOB is present" do 
    @person.DOB = nil 
    it {should be_invalid} 
    end 
end 

, 나는 수를 나는 이름없는 테스트를 제거하면 다음이 @person.DOB = nil

에 crashs @person.names = []

그것은 @person가전에 설정되지 않고, 어떻게 든처럼 보이는이 라인 블록. 누락 된 RSpec의 몇 가지 단서가 있습니까?

편집 : 사람 모델 :

class Person < ActiveRecord::Base 
    has_and_belongs_to_many :names 
    validates_presence_of :DOB 
end 

아주 간단합니다. 이름과 사람을 연결하는 names_people 조인 테이블이 있습니다. 말했듯이 나는 보통 names=()을 통해 이름에 액세스 할 수 있습니다. 이것은 하나의 테스트에 관한 것입니다.

+0

가'('Person.new 수 Person.new' 안 : 이름 => [Factory.create (이 대신

describe "when no name is present" do before { @person.names = [] } it {should be_invalid} end 

예를 들어,이를 사용했다 : name)], : DOB => Date.today)'? – ScottJShea

+1

'rake db : test : prepare'을 실행했는지 확인하십시오 - 때로는 액티브 레코드가 마이그레이션 후에 테스트 DB에서 컬럼을 찾을 수 없을 때 가끔옵니다. –

+0

안녕하세요, Person 모델에 대한 자세한 정보를 제공해 주실 수 있습니까? names 속성에 접근자가 없으므로 메서드 이름 =()이 없습니다. –

답변

0

해결되었습니다. @person을 변경하려면 before 메서드를 다시 사용해야했습니다.

describe "when no name is present" do 
    @person.names = [] 
    it {should be_invalid} 
end