簡體   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