我正在关注《使用 Rails 进行敏捷 Web 开发》(第 4 版)这本书,但我遇到了一个失败的测试用例的麻烦。
我写了一个测试,检查当您将重复的商品添加到购物车时,商品的数量没有变化,但数量会发生变化
test 'adding duplicate products to cart' do
cart = Cart.new
ruby_book = products(:ruby)
cart.add_product ruby_book.id
assert cart.line_items.length == 1
assert cart.total_price == ruby_book.price
p '-------start--------'
p 'outside add_product'
p "line_items: #{cart.line_items}"
cart.add_product ruby_book.id
assert cart.line_items.length == 1 # TEST FAILS HERE with length == 2
assert cart.total_price == ruby_book.price * 2
end
它基本上测试了add_product
方法,即:
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
def add_product(product_id)
p 'inside add_product'
p "line_items: #{line_items}"
p "product_id: #{product_id}"
current_item = line_items.find_by_product_id(product_id)
p "current_item: #{current_item}"
p '---------end---------'
if current_item
current_item.quantity += 1
current_item.price += current_item.product.price
else
current_item = line_items.build(product_id: product_id)
current_item.price = current_item.product.price
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.price }
end
end
为了调试测试,我添加了一些打印调用(如您所见)。输出是:
"-------start--------"
"outside add_product"
"line_items: [#<LineItem id: nil, product_id: 207281424, cart_id: nil, created_at: nil, updated_at: nil, quantity: 1, price: #<BigDecimal:7fa491744fe0,'0.4999E2',18(45)>>]"
"inside add_product"
"line_items: [#<LineItem id: nil, product_id: 207281424, cart_id: nil, created_at: nil, updated_at: nil, quantity: 1, price: #<BigDecimal:7fa491744fe0,'0.4999E2',18(45)>>]"
"product_id: 207281424"
"current_item: "
"---------end---------"
我可以看到line_items
有一个项目,product_id: 207281424
但由于某种原因,调用line_items.find_by_product_id(product_id)
不会返回项目,而是返回nil
. 为什么?