在使用《Agile Development with Ruby on Rails》一书学习RoR时,使用assert_equal和.join方法未能通过单元测试

发布于 2024-11-27 00:46:30 字数 1621 浏览 0 评论 0原文

我刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-12-04 00:46:30

尝试像这样使用 assert_match 而不是 assert_equal...

assert_match /has already been taken/, product.errors[:title].join('; ')

您走在正确的道路上。

编辑:如果assert_match要折旧,

assert_not_nil /has already been taken/.match(product.errors[:title].join('; ')

应该产生相同的结果

Instead of assert_equal, try using assert_match like this...

assert_match /has already been taken/, product.errors[:title].join('; ')

You're on the right track.

EDIT: If assert_match is to be depreciated,

assert_not_nil /has already been taken/.match(product.errors[:title].join('; ')

should yield the same results

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文