rSpec model validations testing
Posted on August 22, 2007 at 04:11 PM
If you find your model specs looking a little unDRY (repetitive) while testing the model validations, you may use a block to DRY your code.
So you can replace this:
it "should validates presence of first_name" do @user.should have_error_on(:first_name, :blank) end it "should validates presence of last_name" do @user.should have_error_on(:last_name, :blank) end it "should validates presence of login" do @user.should have_error_on(:login, :blank) end ...
With this:
%w(first_name last_name login email password_hash).each do |attrib|
it "should validates presence of #{attrib}" do
@user.should have_error_on(attrib, :blank)
end
end
Hope that helps!
PS. “have_error_on” is not standard in rSpec, but only a helper method.
Comments
There are 2 comments on this post. Post yours →
That’s what I call a time-saver!
I just stumbled upon your post. That is a very clean way to save time and make your tests more understandable. You should say little more about the haveerroron helper method.
Post a comment
Required fields in bold.