Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test which validation failed in ActiveRecord?

I have a model like this:

class User < ActiveRecord::Base   validates_length_of :name, :in => (2..5) end 

I want to test this validation:

it "should not allow too short name" do   u = User.new(:name => "a")   u.valid?   u.should have(1).error_on(:name) end 

But then it does not test which kind of error was set on name. I want to know, if it was too_short, too_long, or maybe some other validation failed.

I can lookup the message text in errors array, like this:

u.errors[:name].should include(I18n.t("activerecord.errors.models.user.attributes.name.too_short")) 

But this will fail when I set activerecord.errors.messages.too_short in locale file instead of model-specific message.

So, is it possible to check which kind of error occured?

like image 421
Jan Dudek Avatar asked Nov 07 '10 19:11

Jan Dudek


People also ask

How does validate 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 err validation?

A validation error occurs when you have validation/response checking turned on for one of the questions and the respondent fails to answer the question correctly (for numeric formatting , required response).

What is validate in Ruby on Rails?

Validations are used to ensure that only valid data is saved into your database. For example, it may be important to your application to ensure that every user provides a valid email address and mailing address. Model-level validations are the best way to ensure that only valid data is saved into your database.

What is Activerecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.


1 Answers

Rails added an method to query for errors to ActiveModel in late 2011 and Rails v3.2. Just check to see if the appropriate error has been #added?:

# An error added manually record.errors.add :name, :blank record.errors.added? :name, :blank # => true  # An error added after validation record.email = '[email protected]' record.valid? # => false record.errors.added? :email, :taken, value: '[email protected]' # => true 

Note that for validations that are parametrized (e.g. :greater_than_or_equal_to) you'll need to pass the value of the parameter too.

record.errors.add(:age, :greater_than_or_equal_to, count: 1) record.errors.added?(:age, :greater_than_or_equal_to, count: 1) 

Errors are identified by their i18n key. You can find the appropriate keys to check in the appropriate Rails i18n file for any language under the error section.

Some other nifty questions you can ask ActiveModel#Error are #empty? and #include?(attr), as well as anything you can ask an Enumerable.

like image 69
fny Avatar answered Sep 26 '22 15:09

fny