在使用《Agile Development with Ruby on Rails》一书学习RoR时,使用assert_equal和.join方法未能通过单元测试
我刚刚开始学习 Ruby on Rails,甚至是 Ruby 语言本身。 阅读迭代 B2:模型的单元测试并进行以下练习后: 1. 验证选项 :length 检查模型属性的长度。向 Product 模型添加验证,以检查标题是否至少有 10 个字符长。 2. 更改与您的验证之一相关的错误消息。
我将以下代码放入 /models/product.rb
validates_uniqueness_of :title, :message => 'has already been taken'
validates_length_of :title, :minimum => 8, :message => 'must be at least 8 letters'
并将以下代码放入 /unit/product_test.rb
test "product is not valid without a 8 letters title" do
product = Product.new(:title =>"12345678",
:description => "yyy",
:price => 1,
:image_url => "fred.gif")
product.title = "abcdefg"
assert product.invalid?
assert_equal "must be at least 8 letters", product.errors[:title].join('; ')
product.title = "abcdefgh"
assert product.valid?
end
test "product is not valid without a unique title" do
product = Product.new(:title => products(:lighting).title,
:description => "yyy",
:price => 1,
:image_url => "fred.gif")
assert !product.save
assert_equal "has already been taken", product.errors[:title].join('; ')
end
但当我运行 rake 测试时,出现故障,我不知道如何解决它,它显示:
1) Failure:
test_product_is_not_valid_without_a_unique_title(ProductTest)
/Users/youngoo/Development/RubyonRails/anaheim/test/unit/product_test.rb:66]:
<"has already been taken"> expected but was
<"has already been taken; must be at least 8 letters">.
这是如何发生的?我该怎么办? 我认为问题与 join 方法有关 谢谢!
I just started learning Ruby on Rails, even Ruby language itself.
after reading the Iteration B2: Unit Testing of Models and doing the following exercise:
1. The validation option :length checks the length of a model attribute. Add validation to the Product model to check that the title is at least ten char- acters long.
2. Change the error message associated with one of your validations.
I put the following code into /models/product.rb
validates_uniqueness_of :title, :message => 'has already been taken'
validates_length_of :title, :minimum => 8, :message => 'must be at least 8 letters'
and put following cods into /unit/product_test.rb
test "product is not valid without a 8 letters title" do
product = Product.new(:title =>"12345678",
:description => "yyy",
:price => 1,
:image_url => "fred.gif")
product.title = "abcdefg"
assert product.invalid?
assert_equal "must be at least 8 letters", product.errors[:title].join('; ')
product.title = "abcdefgh"
assert product.valid?
end
test "product is not valid without a unique title" do
product = Product.new(:title => products(:lighting).title,
:description => "yyy",
:price => 1,
:image_url => "fred.gif")
assert !product.save
assert_equal "has already been taken", product.errors[:title].join('; ')
end
but when i run rake test, there's a failure i can't figure out how to solve it, it shows:
1) Failure:
test_product_is_not_valid_without_a_unique_title(ProductTest)
/Users/youngoo/Development/RubyonRails/anaheim/test/unit/product_test.rb:66]:
<"has already been taken"> expected but was
<"has already been taken; must be at least 8 letters">.
how that happen? and how i can do with it?
i thought the problem is related to the join method
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样使用
assert_match
而不是assert_equal
...您走在正确的道路上。
编辑:如果
assert_match
要折旧,应该产生相同的结果
Instead of
assert_equal
, try usingassert_match
like this...You're on the right track.
EDIT: If
assert_match
is to be depreciated,should yield the same results