简体   繁体   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)

I'm new and trying to learn Rails using the book Agile Web Development with Rails, Fourth Edition (for Rails 3.2). 我是新手,并尝试使用Agile Web Development with Rails,第四版 (适用于Rails 3.2)来学习Rails。 Been able to get through all of the chapters so far without hiccups. 到目前为止能够通过所有章节没有打嗝。 If there were errors, it was usually my sloppy code (forgetting a comma, 'end' statement, etc.). 如果有错误,通常是我的草率代码(忘记逗号,'结束'语句等)。 But now I've hit a snag in the chapter on Unit Testing for Models. 但现在我在关于模型单元测试的章节中遇到了麻烦。 At the part where we're validating that the image URL ends with either .gif, .jpg, or .png. 在我们验证图片网址以.gif,.jpg或.png结尾的部分。

I copied the code verbatim from the book for the depot/test/product_test.rb file: 我从书中逐字复制了代码,用于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

But when I run the rake test:units command, I get the following failure... 但是当我运行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!

Does this mean that the image URL it's testing is invalid? 这是否意味着它正在测试的图片网址无效? Why is the test failing if what it says "fred.gif shouldn't be invalid" is correct? 如果“fred.gif不应该无效”的说法是正确的,为什么测试失败?

I'm pretty confident that it's this part of the test that must be incorrect because the other tests I have in there (ex. "product attributes must not be empty", "product price must be positive", etc.) run just fine. 我非常有信心,测试的这一部分必须是不正确的,因为我在那里进行的其他测试(例如“产品属性不能为空”,“产品价格必须为正”等)运行得很好。 I get no failures if I take out the "test image url" code block. 如果我拿出“测试图像网址”代码块,我就不会失败。

Please let me know what I'm doing wrong. 请让我知道我做错了什么。 If you need me to post the entirety of the ProductTest, I can. 如果您需要我发布完整的ProductTest,我可以。


UPDATE : There was a typo in my Products model that was causing the test to fail. 更新 :我的Products模型中有一个错误导致测试失败。 All fixed now. 全部修复了。

I had the same problem and I had to change my definition for new_product. 我有同样的问题,我不得不改变我对new_product的定义。 In my initial definition, I had quotation marks around 'image url'. 在我最初的定义中,我在'image url'周围有引号。 Once I removed the quotes, I was fine. 一旦我删除了引号,我就没事了。 Here's the code (my initial mistake was on the fifth line of my code): 这是代码(我最初的错误是在我的代码的第五行):

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

As you can see, I didn't use the '_' in my test name and my tests passed. 正如您所看到的,我没有在我的测试名称中使用'_'并且我的测试通过了。

I believe the problem is in the definition for function 'new_product'. 我认为问题在于函数'new_product'的定义。 Make sure the function is setting all fields to valid data. 确保该功能正在将所有字段设置为有效数据。 Mine wasn't setting the description to a valid value. 我没有将描述设置为有效值。 I hope this helps. 我希望这有帮助。 You probably already found this solution already but didn't update your post. 您可能已经找到了此解决方案但未更新您的帖子。

What I'm saying is that the product is failing due to some other field. 我所说的是该产品因其他领域而失败。 The test that's failing is checking that there are no errors in a product constructed with each image_url. 失败的测试是检查每个image_url构造的产品中是否存在错误。 You just need to check that the constructor function, new_product, can construct a valid product. 您只需要检查构造函数new_product是否可以构造有效的产品。

Seems like you are missing underscore _ 好像你缺少下划线_

test "image url" do 测试“图像网址”

should be 应该

test "image_url" do 测试“image_url”做

You can the paragraph from my file here: 您可以在我的文件中的段落:

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

I was just struggling with this. 我只是在努力解决这个问题。 There was also a typo in model. 模特中也有一个拼写错误。 With my validations. 我的验证。 I grouped the \\Z with the png . 我将\\Zpng分组。 It needs to be outside the capture group. 它需要在捕获组之外。

wrong: 错误:

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

right: 对:

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