Actually, this is not a characteristic of the test environment, but rather ActiveRecord.
When creating an object using ActiveRecord, you can assign checks to ensure certain things about the attributes of objects (as you have on your object). However, these checks only start at a specific time, and as you can see, the βnewβ method is not one of these times. However, asking about the validity of an object with an invalid? method, you thereby invoked checks.
I think it is more natural to use the "create" method instead of the "new" method to run validation for your object. automatically create validation checks that eliminate your call to "invalid?" in your test and should still fill out the error hash code as desired:
product = Product.create(:title => "My lady", :description => "yyy", :price => 1 ) assert_equal "must be atleast 10 characters long.", product.errors[:title].join('; ')
Similar to the 'create' method, create is created! a method that will actually throw an exception if any validation fails. create will simply return false and populate the error hash code.
For more information about checks, check out: http://guides.rubyonrails.org/active_record_validations_callbacks.html
source share