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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值