I have built a simple rails app with three classes that inherit from ActiveRecord so far. I am a little bit ahead on my test driven development as I’ve already written the classes I need to test. Thus far the tests that I’ve written have all gone well. Objects are being created, attributes are responding, validations are all in place.
The problem begins when I invoke the “!” on my create methods, since now the objects are being passed all the way along to the database. Instead of my normal validations intercepting the invalid inputs it seems the database is being given a chance to respond to the stimulus.
Thus far the error which I’ve received when running my tests is:
ActiveRecord::RecordInvalid:
Validation failed: Shop is too long (maximum is 11 characters)
I’m glad that I am getting this error but would like to know how to plan for it better in my tests. The code that generates the above error is:
it "should not a accept a department with a shopify shop_id that's over 11 chars" do
long_id = "9" * 12
long_id_department = Department.create!(@attr.merge(:id => [long_id, ""]))
long_id_department.should_not be_valid
end
I presume that this line in particular should assume some other form: long_id_department.should_not be_valid
Hoping you all could shed some light on what that form should be.
The shoulda gem makes testing your validations a piece of cake. The more recent shoulda releases are built as an add-on to RSpec.
The documentation for the specific type of validation you’re attempting is here.
You’ll end up with test code that looks like this.