mercredi 3 janvier 2018

Agile Web Development with Rails 5,test failure

Hi I run test and it seems he is failing i can't found what is particulary wrong. here is message:

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

So he doesn't delete. I looked On model/product.rb,as well everything seems fine as it should be in book

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

Well also i recheked line on test where it's failing,count understand what wrong.

  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

Aucun commentaire:

Enregistrer un commentaire