Using RSpec Custom Matchers to Test Validations
Remarkable is a pretty cool way to easily test validations in your Rails models, but I'm a little uncomfortable with how it tests. I believe it only looks in the model to see if you have called the specific validation; it doesn't actually test if a record is valid or not.
For example, AuthLogic does some magic and automatically adds validations to your user model in the background. If testing for presence_of, Remarkable will give you false failures because the User model does not contain "validates_presence_of" in the model. I wonder if Remarkable won't correctly test sub-classed models either?
Instead, I like to build my own custom matchers that use Factory Girl to test for presence_of:
Spec::Matchers.define :require do |attribute| match do |object| factory = object.class.name.downcase.to_sym object = Factory.build(factory, attribute => "") !object.valid? endend describe User do it "should be valid given valid attributes" do user = Factory.build(:user) user.should be_valid end it {should require(:email)} it {should require(:password)} it {should require(:password_confirmation)}endCheck out how simple it is to write custom matchers on the RSpec Wiki.