2012-08-03 3 views
6

나는이 쉬운 검증에 대해 머리를 터뜨 렸으며 유효성을 검사 할 수 없습니다.Rspec 및 FactoryGirl 수락 확인

describe "event has many attendances" do 
    it "should have attendances" do 
     event = FactoryGirl.create(:event) 
     user1 = FactoryGirl.create(:user, firstname: "user1", email: "[email protected]") 
     user2 = FactoryGirl.create(:user, firstname: "user2", email: "[email protected]") 

     attendance1 = FactoryGirl.create(:attendance, event: event, user: user1, terms_of_service: true)  
    end 
    end 

이 오류를 가지고 있지만하지 않습니다한다 : 이것은 내 테스트입니다

factory :attendance do 
    user 
    event 
    terms_of_service true 
    end 

: 이것은 내 공장

class Attendance < ActiveRecord::Base 
    belongs_to :user, counter_cache: true 
    belongs_to :event, counter_cache: true 

    validates :terms_of_service, :acceptance => true 
end 

이다 : 나는 다음과 같은 모델을 가지고 .

Running spec/models/workshop_spec.rb 
.............F 

Failures: 

    1) Event event has many attendances should have attendances 
    Failure/Error: attendance1 = FactoryGirl.create(:attendance, event: event, user: user1, terms_of_service: true) 
    ActiveRecord::RecordInvalid: 
     Validation failed: Terms of service must be accepted 
    # ./spec/models/event_spec.rb:33:in `block (3 levels) in <top (required)>' 

브라우저에서 이러한 작업을 수행 할 때 모든 항목이 정상적으로 처리됩니다. 나는 무엇을 여기에서 놓치고 있냐?!

답변

9

:terms_of_service은 db 열로 매핑됩니까? validates :acceptance의 기본값은 true이 아니라 "1"문자열입니다. 이 열을 dB로 매핑 된 경우 , 검증에 :accept => true를 추가하려고 :

필드가 매핑되지, 또는 DB 열이 부울없는 경우
validates :terms_of_service, :acceptance => {:accept => true} 

는, 테스트 및 공장에서 진정한 대신에 "1"을 사용하려고 .

+0

감사합니다. 나는 문자열을 제외한 모든 것을 시도했다. 감사합니다, 기록을 위해 그것은 사실상의 속성이었습니다! –

+0

@dimuch 당신이 내 영웅이라는 것을 당신은 알고 있습니까? 감사. – Jeff

관련 문제