views:

73

answers:

5

Everybody knows that automated testing is a good thing.

Not everybody knows exacly what to test.

My question is if native validations like validate_presence_of, validate_uniqueness_of and so on should be tested in the application.

In my office we are three, one thinks it should be tested, one thinks it shouldn´t and I am up in the air.

+3  A: 

Are you planning to write unit tests for every single Ruby operator and API method as well?

Your unit tests should your own code, not other people's - that's their job, why duplicate their work? And if you don't trust them to do their job well, why are you using their code?

Michael Borgwardt
+5  A: 

Yes.

Testing that a model attribute is present or not is only testing the validates_presence_of code as a by-product of the real test which is that the validates_presence_of exists within your model.

If someone commented out a bunch of validation code and then forgot to uncomment it then this would go undetected and could cause all sorts of problems.

I test them, not because I think they don't work but to ensure that they are present in my model when required.

Steve Weet
> I test them, not because I think they don't work but to ensure that they are present in my model when required
Simone Carletti
+1  A: 

Test the code you write. ActiveRecord has great test coverage including coverage for the validation class methods.

Spec:

require 'spec_helper'

describe User do
  before(:each) do
    @user = User.new
  end

  it "should not be valid without an email" do
    @user.save.should be_false
    @user.should_not be_valid
    @user.email = "[email protected]"
    @user.should be_valid
    @user.save.should be_true
  end

end

To get that spec to pass you would need

class User < ActiveRecord::Base
  validates_presence_of :email
end
Steve Graham
You are actually taking this further than I would and testing ActiveRecord itself. If the user is not valid then save will always be false. I would not even test the saving of the record in unit test (with a few exceptions uniqueness and associations being 2 that I might occasionally write to the DB)
Steve Weet
I see your point. I agree in the above expectation, many will say checking the record was not saved is unnecessary. Others will disagree.The idea is to test the behaviour. Sometimes you need to test save returns false, e.g. if you have code like before_save :process_credit_card, :if => :card_attributes_updated? that may add an error to base if a a call to payment gateway returns an invalid repsonse, valid? will be true even though the record may not
Steve Graham
+4  A: 

Matthew Bass has a great gem he's released for just this type of thing. It adds rspec matchers that check to make sure the validation is in place without actually running the underlying ActiveRecord code. Read more about it here.

It adds matchers for validations:

it_should_validate_presence_of     :first_name, :last_name, :email
it_should_validate_numericality_of :zip
it_should_validate_uniqueness_of   :email
it_should_validate_inclusion_of    :gender, :in => %w(Male Female)

Also matchers for associations:

it_should_belong_to :employer
it_should_have_many :friends, :romans, :countrymen
it_should_have_one  :account
it_should_have_and_belong_to_many :comments

And a few other useful additions:

# tests that User.count increases by 1
it_should_be_createable :with => {:first_name => 'Ed', :last_name => 'The Duck', :email => '[email protected]'}

# tests that the attribute is protected
it_should_protect :email

That's not by any means an exhaustive list. I've got a fork where I've added a few others I needed, likely there are others floating around as well. It's a good approach and for me fit the middle ground between ensuring the validations were still in the model, and having to explicitly write tests to execute ActiveRecord code to ensure it.

Jeff Whitmire
+1 this looks very nice indeed.
Steve Weet
This is a good suggestion. It had looked at Shoulda, which makes testing this very easy, but use RSPec (because i like it better for all the rest :), and i really missed something like this.
nathanvda
+1  A: 

This is where tools like Shoulda really come in handy. I think its totally up to you to test how you write the code with the tools people provide you. Just because you are using has_many, does not mean you are using it right!

Seriously, if you integrate Shoulda into the mix, it becomes trivial to test these sorts of things. if you run rcov, its going to tell you all the code you wrote is not fully tested, unless you do.

Derek P.