,expected,but,was,<["has,already,been,taken"]>.">

assert_equal使用I18n.translate时的问题

阅读更多

今天重新在depot运行测试,用静态测试数据测试商品标题的唯一性,发现有个断言过不去:

 

assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title]

错误信息:

 

 

  1) Failure:
test_product_is_not_valid_without_a_unique_title_-_i18n(ProductTest) [E:/works/rubyaptana/rails/test/unit/product_test.rb:67]:
<"has already been taken"> expected but was
<["has already been taken"]>.

5 tests, 24 assertions, 1 failures, 0 errors, 0 skips

 

 

后面发现在是因为没有照Agile Web Development with Rails Fourth Edition书上说的写。product.errors[:title]后还有join("; ")

看日志应该是一个字符串,和一个只有一个字符串元素的数组的区别。

经乱试,发现两种方式都可以通过:

1.

 

assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title].join("")

 2.

assert_equal I18n.translate(['activerecord.errors.messages.taken']), product.errors[:title]

 

product_test.rb的测试方法是这样的:

  test "product is not valid without a unique title - i18n" do
    product = Product.new(title:       products(:ruby).title,
                          description: "yyy", 
                          price:       1, 
                          image_url:   "fred.gif")

    assert product.invalid?
    assert_equal I18n.translate(['activerecord.errors.messages.taken']), product.errors[:title]
    #assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title].join("")
   end

 

 

你可能感兴趣的:(ruby,on,rails,rails,I18n.translate,assert_equal,<"has,already,been,taken">,expected,but,was,<["has,already,been,taken"]>.)