Rspec & FactoryGirl Compliance Check

I am bothering myself with this simple check, and I cannot check it. I have the following model:

class Attendance < ActiveRecord::Base belongs_to :user, counter_cache: true belongs_to :event, counter_cache: true validates :terms_of_service, :acceptance => true end 

This is my Factory:

  factory :attendance do user event terms_of_service true end 

This is my test:

  describe "event has many attendances" do it "should have attendances" do event = FactoryGirl.create(:event) user1 = FactoryGirl.create(:user, firstname: "user1", email: " mail@user2.nl ") user2 = FactoryGirl.create(:user, firstname: "user2", email: " mail@user1.nl ") attendance1 = FactoryGirl.create(:attendance, event: event, user: user1, terms_of_service: true) end end 

This should not cause any errors, but it does.

 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)>' 

When I do these actions in my browser, and I agree that everything is going well. What am I missing here ?!

+6
source share
1 answer

Is :terms_of_service mapped to a db column? The default value for validates :acceptance is the string "1", not true . If it matches the db column, try adding :accept => true to the check:

 validates :terms_of_service, :acceptance => {:accept => true} 

If the field is not displayed, or the database column is not logical, try using "1" instead of true in tests and factories.

+10
source

Source: https://habr.com/ru/post/922021/


All Articles