首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Rails进行敏捷Web开发4次迭代E3单元测试

使用Rails进行敏捷Web开发4次迭代E3单元测试
EN

Stack Overflow用户
提问于 2016-01-15 21:25:12
回答 1查看 137关注 0票数 1

我是Ruby on Rails的新手,正在读"Agile Web Development with Rails 4“这本书。在做第10章末尾的“游戏时间”练习(Iteration E3 - Finishing the Cart)时,我偶然发现了一些问题。

其中之一是在第二个练习中,其中一个人应该创建单元测试来向购物车添加独特的和重复的产品。当一个人将一个产品添加到购物车中时,它可能是该类型的第一个产品,因此数量是1,但每个额外的添加操作都会增加数量。这在浏览器测试中工作得很好,但是我的测试用例失败了。

测试用例:

代码语言:javascript
复制
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的代码:

代码语言:javascript
复制
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。如果你需要任何额外的代码,请让我知道。

EN

回答 1

Stack Overflow用户

发布于 2016-01-23 04:13:11

我终于找到了我的错误:

数量肯定会在模型中增加,但不会保存在模型中。它保存在控制器中,所以在我的测试中完全绕过了这一步,因为它只是测试模型本身。

为了修复测试,我将cart.add_product(item.id, item.price)更改为cart.add_product(item.id, item.price).save以修复该问题。

我还在测试前重新加载了购物车,因为否则它会计算旧的总价(感谢@PrakashMurthy,虽然它应该解决另一个问题,但在最后它有帮助:-)。

现在,(工作的)测试用例如下所示:

代码语言:javascript
复制
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)

  # the following two lines do the trick:
  cart.add_product(item.id, item.price).save
  cart.reload

  assert cart.save
  assert_equal 1, cart.line_items.count, 'duplicate saved as new line'
  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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34812096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档