繁体   English   中英

为什么我的image_url单元测试失败了? (来自Agile Web Development with Rails'Depot'教程)

[英]Why is my image_url Unit Test failing? (from Agile Web Development with Rails 'Depot' tutorial)

我是新手,并尝试使用Agile Web Development with Rails,第四版 (适用于Rails 3.2)来学习Rails。 到目前为止能够通过所有章节没有打嗝。 如果有错误,通常是我的草率代码(忘记逗号,'结束'语句等)。 但现在我在关于模型单元测试的章节中遇到了麻烦。 在我们验证图片网址以.gif,.jpg或.png结尾的部分。

我从书中逐字复制了代码,用于depot / test / product_test.rb文件:

test "image url" do
ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif }
bad = %w{ fred.doc fred.gif/more fred.gif.more }

ok.each do |name|
    assert new_product(name).valid?, "#{name} shouldn't be invalid"
end
bad.each do |name|
    assert new_product(name).invalid?, "#{name} shouldn't be valid"
end

但是当我运行rake test:units命令时,我得到以下失败......

1) Failure:
test_image_url(ProductTest)[../depot/test/unit/product_test.rb:46]:
fred.gif shouldn't be invalid

4 tests, 13 assertions, 1 failures, 0 errors, 0 skips
rake aborted!

这是否意味着它正在测试的图片网址无效? 如果“fred.gif不应该无效”的说法是正确的,为什么测试失败?

我非常有信心,测试的这一部分必须是不正确的,因为我在那里进行的其他测试(例如“产品属性不能为空”,“产品价格必须为正”等)运行得很好。 如果我拿出“测试图像网址”代码块,我就不会失败。

请让我知道我做错了什么。 如果您需要我发布完整的ProductTest,我可以。


更新 :我的Products模型中有一个错误导致测试失败。 全部修复了。

我有同样的问题,我不得不改变我对new_product的定义。 在我最初的定义中,我在'image url'周围有引号。 一旦我删除了引号,我就没事了。 这是代码(我最初的错误是在我的代码的第五行):

def new_product(image_url)
  Product.new(title:       "My Book Title",
               description: "yyy",
               price:       1,
               image_url:   image_url)
end

test "image url" do
  ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
         http://a.b.c/x/y/z/fred.gif }
  bad = %w{ fred.doc fred.gif/more fred.gif.more }

  ok.each do |name|
    assert new_product(name).valid?, "#{name} shouldn't be invalid"
  end

  bad.each do |name|
    assert new_product(name).invalid?, "#{name} shouldn't be valid"
  end
end

正如您所看到的,我没有在我的测试名称中使用'_'并且我的测试通过了。

我认为问题在于函数'new_product'的定义。 确保该功能正在将所有字段设置为有效数据。 我没有将描述设置为有效值。 我希望这有帮助。 您可能已经找到了此解决方案但未更新您的帖子。

我所说的是该产品因其他领域而失败。 失败的测试是检查每个image_url构造的产品中是否存在错误。 您只需要检查构造函数new_product是否可以构造有效的产品。

好像你缺少下划线_

测试“图像网址”

应该

测试“image_url”做

您可以在我的文件中的段落:

test "image_url" do
ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg   http://a.b.c/x/y/z.gif}
bad = %w{ fred.doc fred.gif/more fred.gif.more }
ok.each do |name|
    assert new_product(name).valid?, "#{name} shouldn't be invalid"
end
bad.each do |name|
    assert new_product(name).invalid?, "#{name} shouldn't be valid"
end

我只是在努力解决这个问题。 模特中也有一个拼写错误。 我的验证。 我将\\Zpng分组。 它需要在捕获组之外。

错误:

class Product < ApplicationRecord
  validates :title, :description, :image_url, presence: true
  validates :price, numericality: { greater_than_or_equal_to: 0.01 }
  validates :title, uniqueness: true
  validates :image_url, allow_blank: true, format: {
    with: %r{\.(gif|jpg|png\Z)}i, # <<<<<----
    message: 'must be a URL for GIF, JPG, or PNG image.'
  }
end

对:

class Product < ApplicationRecord
  validates :title, :description, :image_url, presence: true
  validates :price, numericality: { greater_than_or_equal_to: 0.01 }
  validates :title, uniqueness: true
  validates :image_url, allow_blank: true, format: {
    with: %r{\.(gif|jpg|png)\Z}i, # <<<<<----
    message: 'must be a URL for GIF, JPG, or PNG image.'
  }
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM