這裏是我的控制器規格Rspec的方法被稱爲2X,但無法找到第二次
before do
@order = Order.new
end
it "should call find & assign_attributes & test delivery_start methods" do
Order.should_receive(:find).with("1").and_return(@order)
Order.any_instance.should_receive(:assign_attributes).with({"id"=>"1", "cancel_reason" => "random"}).and_return(@order)
Order.any_instance.should_receive(:delivery_start).and_return(Time.now)
post :cancel, order: {id:1, cancel_reason:"random"}
end
失敗是這樣的:
Failure/Error: Unable to find matching line from backtrace
(#<Order:0x007fdcb03836e8>).delivery_start(any args)
expected: 1 time with any arguments
received: 2 times with any arguments
# this backtrace line is ignored
但我不知道爲什麼delivery_start
是基於這個控制器動作被稱爲兩次:
def cancel
@order = Order.find(cancel_params[:id])
@order.assign_attributes(cancel_params)
if (@order.delivery_start - Time.now) > 24.hours
if refund
@order.save
flash[:success] = "Your order has been successfully cancelled & refunded"
redirect_to root_path
else
flash[:danger] = "Sorry we could not process your cancellation, please try again"
render nothing: true
end
else
@order.save
flash[:success] = "Your order has been successfully cancelled"
redirect_to root_path
end
end
對不起,但它「應該調用find&assign_attributes&test delivery_start方法」做 Order.should_receive(:find).with(「1」)。and_return(@order ) Order.any_instance.should_receive(:assign_attributes).with({「id」=>「1」,「cancel_reason」=>「random」})。and_return(@order)Order.any_instance.should_receive(:delivery_start) .and_return(Time.now) post:取消,order:{id:1,cancel_reason:「random」} end –
我不確定,但我會猜測'delivery_start'可能合理地顯示在視圖正在顯示?這將是我猜測第二個「呼叫」來自何處。 –
@TarynEast沒有相關的視圖,調用'delivery_start' – james