开发者

Cannot remove item from cart. Spec is failing

I have a simple shopping cart app and I am trying to test the following method:

  def add(item_id)
    item = Product.find(item_id)
    args = {
      :product_id=>item.id,
      :seller_id=>item.shop_id,
      :price =>item.price
    }
    cart_items<<CartItem.create(args)
  end

  def remove(item_id)
    cart_items.where(:product_id=>item_id).map(&:destroy)
  end

The spec for this thing is

  it "should remove a product from the cart" do
    cart = Cart.new
    item = Product.create(:price=>3450,:id=>1,:shop_id=>1)
    cart.add(item.id)    
    cart.s开发者_StackOverflow社区hould_not be_empty

    cart.remove(item.id)
    cart.should be_empty
  end

No matter what I do, I cannot get it to pass. The cart_item.length is always equal to 1. Not sure why this is happening. Please help.


I suspect the problem is that you aren't saving the Cart to the database at any point.


I've often found that this is because you aren't reloading from the database after removing the item.

cart.reload
cart.should be_empty
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜