Rails - Test :: Unit - 测试该数字大于0

时间:2010-12-13 19:56:38

标签: ruby-on-rails unit-testing ruby-on-rails-3

我的帖子模型有一个序列属性,我想确保它在数字上大于0:

class Post < ActiveRecord::Base

  validates :sequence, :presence => true,
                       :uniqueness => true,
                       :numericality => { :greater_than => 0 }

大。现在我正在尝试进行以下测试。

  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

但是输出失败了:

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">.

显然,rails不喜欢以下行'activerecord.errors.messages.greater_than'

那么我需要通过 I18n.translate 方法来测试序列是否大于0?

PS: 我在this page上阅读了标题为 5.1.2错误消息插值的部分,并认为我的代码可以正常工作,但显然我错了。

0 个答案:

没有答案