0
我測試我的網上商店應用使用RSpec,這裏是我在做什麼:需要重新加載current_cart獲得測試通過
# spec/controllers/line_items_controller_spec.rb
require 'spec_helper'
describe LineItemsController do
describe "POST 'create'" do
before do
@current_cart = Factory(:cart)
controller.stub!(:current_cart).and_return(@current_cart)
end
it 'should merge two same line_items into one' do
@product = Factory(:product, :name => "Tee")
post 'create', {:product_id => @product.id}
post 'create', {:product_id => @product.id}
assert LineItem.count.should == 1
assert LineItem.first.quantity.should == 2
end
end
end
# app/controllers/line_items_controller.rb
class LineItemsController < ApplicationController
def create
current_cart.line_items.each do |line_item|
if line_item.product_id == params[:product_id]
line_item.quantity += 1
if line_item.save
render :text => "success"
else
render :text => "failed"
end
return
end
end
@line_item = current_cart.line_items.new(:product_id => params[:product_id])
if @line_item.save
render :text => "success"
else
render :text => "failed"
end
end
end
現在的問題是,它從不加起來有兩個line_items因爲第二次我進入line_items_controller#create
,current_cart.line_items
是[],我已經運行current_cart.reload
來讓測試通過,任何想法出了什麼問題?
我注意到了存根,它也不工作。 '@ line_item.save'返回true – leomayleomay 2011-03-14 09:43:16
@leomayleomay inital current_cart.line_items在哪裏設置? – lebreeze 2011-03-14 11:21:23
購物車has_many line_items,我認爲這是最初的current_cart.line_items設置 – leomayleomay 2011-03-14 11:46:26