2011-05-20 79 views
0

我有一個簡單的購物車的應用程序,我想測試下面的方法:無法從購物車中移除物品。規格失敗

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 

對於這件事情的規範是

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.should_not be_empty 

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

不管我做什麼,我不能讓它通過。 cart_item.length總是等於1.不知道爲什麼會發生這種情況。請幫忙。

回答

0

我懷疑問題是您沒有在任何時間將購物車保存到數據庫。

0

我經常發現,這是因爲您沒有在刪除項目後從數據庫重新加載。

cart.reload 
cart.should be_empty 
+0

我現在收到此錯誤:無法找到沒有ID的購物車 – picardo 2011-05-20 23:03:37

+0

不知道你從哪裏得到錯誤 - 回溯是否告訴你?發現在哪裏發生? – 2011-05-20 23:13:06

+0

我正在使用自動測試,所以沒有回溯。 :(但錯誤來自發生重新調用的線路 – picardo 2011-05-20 23:18:00

相關問題