使用Rails 5进行敏捷Web开发,测试失败

时间:2018-01-03 21:02:55

标签: ruby-on-rails testing tdd

嗨,我跑了测试,似乎他失败了,我找不到什么特别的错。这是消息:

Failure:
ProductsControllerTest#test_can't_delete_product_in_cart [/home/magvin/work/depot/test/controllers/products_controller_test.rb:53]:
"Product.count" didn't change by 0.
Expected: 3
  Actual: 2

所以他不删除。我看了模型/ product.rb,一切看起来都很好,因为它应该在书中

class Product < ApplicationRecord
    has_many :line_items
    before_destroy :ensure_not_referenced_by_any_line_item

    validates :title, :description, :image_url, presence: true
    validates :price, numericality: {greater_than_or_equal_to: 0.01, message: "%{value} seems wrong"}
    validates :title, uniqueness: true
    validates :image_url, allow_blank: true, format: { 
        with: %r{\.(gif|jpg|png)\z}i,
        message: 'must be GIF, JPG or PNG file.'
    } #i stands for case insensitive
    validates :title, :length => {:minimum => 2}
    validates :description, :length => { :in => 3..150 }

    #...
    #...

    private

    #ensure that there are no line items referencing this product

    def ensure_not_referenced_by_any_line_item
        unless line_items.empty?
            errors.add(:base, 'Line Items Presents')
            throw :abort
        end
    end
end

那么我也会在测试失败的地方重新考虑线路,算一下是什么问题。

  test "can't delete product in cart" do
assert_difference('Product.count', 0) do
  delete product_url(products(:two))
end

assert_redirected_to products_url
end

1 个答案:

答案 0 :(得分:0)

夹具问题:)改变了,一切正常)