Reputation: 10378
There is field name active in customer table. It validates as below in customer.rb:
validates :active, :presence => true
Here is the rspec code to test a field short_name:
it "should be OK with duplicate short_name in different active status" do
customer = Factory(:customer, :active => false, :short_name => "test user")
customer1 = Factory.build(:customer, :active => true, :short_name => "Test user")
customer1.should be_valid
end
Validation for short_name is:
validates :short_name, :presence => true, :uniqueness => { :scope => :active }
The above code causes the error:
1) Customer data integrity should be OK with duplicate short_name in different active status
Failure/Error: customer = Factory(:customer, :active => false, :short_name => "test user")
ActiveRecord::RecordInvalid:
Validation failed: Active can't be blank
# ./spec/models/customer_spec.rb:62:in `block (3 levels) in <top (required)>'
It seems that the false value assigned to field active was considered to be blank or nil by rspec and failed the data validation check. Tried to use 0 for false and it causes the same error. The rspec case passes if removing the validation for field active.
Upvotes: 3
Views: 1012
Reputation: 9172
This is not a rspec issue, it's related to Rails' validation. I suppose your active
field is a boolean and, to quote the validates_presence_of
documentation:
If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true
So simply change your validator to something like the following (assuming you want the "sexy" syntax) and it should work:
validates :active, :inclusion => [true, false]
Upvotes: 6