I've been breaking my head on this easy validation and I can't get it to validate. I've got 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 shouldn't bring up 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 accept the tos all goes well. What am i missing here?!
RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.
RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested.
RSpec is a testing framework, so we first need to understand what a test is. If you're a developer, then you've already performed software tests, whether you realize it or not.
Is :terms_of_service
mapped to db column? The default value for validates :acceptance
is string "1", not true
.
If it is mapped to db column, try to add :accept => true
to validation:
validates :terms_of_service, :acceptance => {:accept => true}
If the field is not mapped, or DB column is not boolean, try to use "1" instead of true in tests and factories.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With