今天重新在depot运行测试,用静态测试数据测试商品标题的唯一性,发现有个断言过不去:
Ruby代码:
-
assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title]
错误信息:
原文参考自站长网:
Log代码:
-
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("; ")
看日志应该是一个字符串,和一个只有一个字符串元素的数组的区别。
经乱试,发现两种方式都可以通过:
Ruby代码:
-
assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title].join("")
Ruby代码:
-
assert_equal I18n.translate(['activerecord.errors.messages.taken']), product.errors[:title]
product_test.rb的测试方法是这样的:
Ruby代码:
-
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]
-
-
end
阅读(1159) | 评论(0) | 转发(0) |