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.
This is not a rspec issue, it’s related to Rails’ validation. I suppose your
activefield is a boolean and, to quote thevalidates_presence_ofdocumentation:So simply change your validator to something like the following (assuming you want the “sexy” syntax) and it should work: