Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should native validations be tested in rails?

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.

like image 856
Ricardo Acras Avatar asked Dec 10 '09 12:12

Ricardo Acras


People also ask

What is the difference between validate and validates in rails?

So remember folks, validates is for Rails validators (and custom validator classes ending with Validator if that's what you're into), and validate is for your custom validator methods.

How does validation work in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.


3 Answers

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.

like image 54
Steve Weet Avatar answered Oct 22 '22 05:10

Steve Weet


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.

like image 25
Jeff Whitmire Avatar answered Oct 22 '22 04:10

Jeff Whitmire


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

Your unit tests should test 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?

like image 5
Michael Borgwardt Avatar answered Oct 22 '22 04:10

Michael Borgwardt