【发布时间】:2016-01-15 13:25:11
【问题描述】:
我是 Ruby on Rails 的新手,正在阅读“使用 Rails 4 进行敏捷 Web 开发”一书。在第 10 章(迭代 E3 - 完成购物车)末尾做“游戏时间”练习时,我偶然发现了一些问题。
其中之一是在第二个练习中,其中应该创建单元测试以将独特和重复的产品添加到某些购物车中。 当一个人将产品添加到该购物车时,它可能是该类型的第一个产品,因此数量是一个,但每个额外的添加操作都会增加数量。这在浏览器测试中运行良好,但我的测试用例失败了。
测试用例:
test 'duplicates must not be saved as a new line item' do
# create cart and add one product
cart = new_cart_with_one_product(:ruby)
assert cart.save
assert_equal 1, cart.line_items.count
assert_equal 1, cart.line_items.find_by(
product_id: products(:ruby).id).quantity
assert_equal 49.50, cart.total_price.to_f
# ----------------------------------------------------------------
# create a second (actually the same product) and add it to cart:
item = products(:ruby)
cart.add_product(item.id, item.price)
assert cart.save
assert_equal 1, cart.line_items.count, 'duplicate saved as new line'
# test FAILS at the next two lines:
assert_equal 2, cart.line_items.find_by(product_id: item.id).quantity,
'quantity has not been increased'
assert_equal 99.00, cart.total_price.to_f, 'total price is wrong'
end
它告诉我期望值为 2,但实际值为 1。 所以数量没有增加。总价格也没有变化,尽管两者都在开发环境中起作用。
这是 Cart-Model 的代码:
class Cart < ActiveRecord::Base
has_many:line_items, dependent: :destroy
def add_product(product_id, product_price)
current_item = line_items.find_by(product_id: product_id)
if current_item
current_item.quantity +=1
else
# create a new line_item
current_item = line_items.build(product_id: product_id,
price: product_price)
end
current_item
end
def total_price
line_items.to_a.sum {|item| item.total_price }
end
end
我在 Ruby 2.2.3 上使用 Rails 4.2.5。
我希望有人可以帮助我,因为我不明白为什么会在测试环境中发生这种情况并且只使用rake test。如果您需要任何其他代码,请告诉我。
【问题讨论】:
-
在使用
cart.reload保存后尝试reloadingcart对象。您可以将失败的测试行更改为assert_equal 2, cart.reload.line_items.find_by...stackoverflow.com/questions/5519741/… 有关于需要reload的解释。 -
感谢@PrakashMurthy,但不幸的是这并没有帮助。我之前试过
cart.line_items(true),它绕过了缓存,但这也无济于事(即使结合你的建议)。
标签: ruby-on-rails unit-testing