My post model has a sequence attribute that I want to make sure is numerically greater than 0:
class Post < ActiveRecord::Base
validates :sequence, :presence => true,
:uniqueness => true,
:numericality => { :greater_than => 0 }
Great. Now I'm trying to make the following test pass.
test "post is not valid if sequence is negative" do
post = Post.new( :title => "Unique Title",
:description => "Some text",
:sequence => -1)
assert !post.save
assert_equal I18n.translate('activerecord.errors.messages.greater_than'),
post.errors[:sequence].join('; ')
end
But it fails with this output:
1) Failure:
test_post_is_not_valid_if_sequence_is_negative(PostTest) [/test/unit/post_test.rb:46]:
<"translation missing: en.activerecord.errors.messages.numericality.greater_than"> expected but was
<"must be greater than 0">.
Obviously rails doesn't like the following line 'activerecord.errors.messages.greater_than'
So what argument do I need to pass the I18n.translate method in order to test that the sequence is greater than 0?
PS: I read the section titled 5.1.2 Error Message Interpolation on this page and thought my code would work, but obviously I've 开发者_运维问答got it wrong.
精彩评论