Chinaunix首页 | 论坛 | 博客
  • 博客访问: 827469
  • 博文数量: 190
  • 博客积分: 2991
  • 博客等级: 少校
  • 技术积分: 2400
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-24 18:11
文章分类

全部博文(190)

文章存档

2015年(3)

2014年(1)

2013年(65)

2012年(121)

我的朋友

分类: Web开发

2013-03-21 12:22:17

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

 

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

错误信息:

 原文参考自站长网:

Log代码:

 

  1.   1) Failure:  
  2. test_product_is_not_valid_without_a_unique_title_-_i18n(ProductTest) [E:/works/rubyaptana/rails/test/unit/product_test.rb:67]:  
  3. <"has already been taken"> expected but was  
  4. <["has already been taken"]>.  
  5.   
  6. 5 tests, 24 assertions, 1 failures, 0 errors, 0 skips  

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

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

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

 

Ruby代码:  

 

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

 

Ruby代码:  

 

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

 

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

Ruby代码:  
  1. test "product is not valid without a unique title - i18n" do  
  2.   product = Product.new(title:       products(:ruby).title,  
  3.                         description: "yyy",   
  4.                         price:       1,   
  5.                         image_url:   "fred.gif")  
  6.   
  7.   assert product.invalid?  
  8.   assert_equal I18n.translate(['activerecord.errors.messages.taken']), product.errors[:title]  
  9.   #assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title].join("")  
  10.  end  
阅读(1131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~