English 中文(简体)
How to test custom messages thrown back by a models validation in Rails
原标题:

I have this validation in my user model.

validates_uniqueness_of :email, :case_sensitive => false,
              :message => "Some funky message that ive cleverly written"

In my tests I want to ensure that when a user enters a dupe email address that my message definately gets shown but without having to duplicate the error string from above in my test. I dont like that because im sure the message will change as i start thinking about copy. Does rails store these error messages - something which i can call in my tests?

Ive done a general test of

assert @error_messages[:taken] , user.errors.on(:email)

but that would pass on any of the other email related errors ive set validations up to catch i.e. incorrect formating, blank etc.

最佳回答

I made a quick test, and it looks like the error messages are sorted in the order you wrote your validation statements in your model class (top-down).

That means, you can find the error message for the first validation on an attribute at the first place in the errors array:

user.errors.on(:email)[0]

So, if your user model class contains something like this:

validates_presence_of   :email
validates_uniqueness_of :email, :case_sensitive => false, :message => "Some funky message that ive cleverly written"
validates_length_of     :email

...you ll find your funky message at user.errors.on(:email)[1], but only if at least validates_presence_of triggers an error, too.

Concerning your specific problem: The only way I could think of to not repeat your error message in the test, is to define a constant in your user model and use this instead of directly typing a message for that validation:

EMAIL_UNIQUENESS_ERROR_MESSAGE = "Some funky message that ive cleverly written"
...
validates_uniqueness_of :email, :case_sensitive => false, :message => EMAIL_UNIQUENESS_ERROR_MESSAGE

In your test, you could use this constant, too:

assert_equal User::EMAIL_UNIQUENESS_ERROR_MESSAGE, user.errors.on(:email)[1]
问题回答

In rspec,

it "should validate uniqueness of email" do
  existing_user = User.create!(:email => email)
  new_user = User.create!(:email => existing_user.email)
  new_user.should_not be_valid
  new_user.errors.on(:email).should include("Some funky message that ive cleverly written")
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签