Skip to content

Instantly share code, notes, and snippets.

@Lordnibbler
Created June 6, 2013 04:10
Show Gist options
  • Select an option

  • Save Lordnibbler/5719252 to your computer and use it in GitHub Desktop.

Select an option

Save Lordnibbler/5719252 to your computer and use it in GitHub Desktop.
require 'spec_helper'
# require 'support/spree/shared_contexts/custom_products'
describe Spree::OrdersController do
let(:user) { create(:user) }
let(:order) { mock_model(Spree::Order, :number => "R123", :reload => nil, :save! => true, :coupon_code => nil, :user => user, :completed? => false, :currency => "USD", :token => 'a1b2c3d4')}
before do
# Don't care about IP address being set here
order.stub(:last_ip_address=)
Spree::Order.stub(:find).with(1).and_return(order)
controller.stub(:try_spree_current_user => user)
end
context "#populate" do
before { Spree::Order.stub(:new).and_return(order) }
it "should create a new order when none specified" do
Spree::Order.should_receive(:new).and_return order
spree_post :populate, {}, {}
session[:order_id].should == order.id
end
context "with Variant" do
let(:populator) { double('OrderPopulator') }
before do
Spree::OrderPopulator.should_receive(:new).and_return(populator)
end
it "should handle single variant/quantity pair" do
populator.should_receive(:populate).with("variants" => { 1 => "2" }).and_return(true)
spree_post :populate, { :order_id => 1, :variants => { 1 => 2 } }
response.should redirect_to spree.cart_path
end
it "should handle multiple variant/quantity pairs with shared quantity" do
populator.should_receive(:populate).with("products" => { 1 => "2" }, "quantity" => "1").and_return(true)
spree_post :populate, { :order_id => 1, :products => { 1 => 2 }, :quantity => 1 }
response.should redirect_to spree.cart_path
end
end
context "buy now" do
let(:big_order) { create :order_with_line_items, line_items_count: 5 }
let(:small_order) { create :order_with_line_items, line_items_count: 1 }
it "should clear @current_order.line_items if any are present" do
# POST one order to #populate
spree_post :populate, { :order_id => big_order.id, :variants => { 1 => 2 } }
assigns(:current_order).line_items.count.should == 5
# POST a buy_now order to #populate, and it should reset @current_order.line_items
spree_post :populate, { :buy_now => true, :order_id => small_order.id, :variants => { 1 => 2 } }
assigns(:current_order).line_items.count.should == 1
end
end
end
context "#update" do
before do
order.stub(:update_attributes).and_return true
order.stub(:line_items).and_return([])
order.stub(:line_items=).with([])
order.stub(:last_ip_address=)
Spree::Order.stub(:find_by_id_and_currency).and_return(order)
end
it "should not result in a flash success" do
spree_put :update, {}, {:order_id => 1}
flash[:success].should be_nil
end
it "should render the edit view (on failure)" do
order.stub(:update_attributes).and_return false
order.stub(:errors).and_return({:number => "has some error"})
spree_put :update, {}, {:order_id => 1}
response.should render_template :edit
end
it "should redirect to cart path (on success)" do
order.stub(:update_attributes).and_return true
spree_put :update, {}, {:order_id => 1}
response.should redirect_to(spree.cart_path)
end
end
context "#empty" do
it "should destroy line items in the current order" do
controller.stub(:current_order).and_return(order)
order.should_receive(:empty!)
spree_put :empty
response.should redirect_to(spree.cart_path)
end
end
# Regression test for #2750
context "#update" do
before do
user.stub :last_incomplete_spree_order
controller.stub :set_current_order
end
it "cannot update a blank order" do
spree_put :update, :order => { :email => "foo" }
flash[:error] = I18n.t(:order_edit)
response.should redirect_to(spree.root_path)
end
end
context "#edit" do
before do
user.stub :last_incomplete_spree_order
controller.stub :set_current_order
end
include_context "custom products"
it "has a @order and @accessories instance variable of proper class" do
spree_get :edit
assigns(:order).should be_a_kind_of Spree::Order
assigns(:accessories).first.should be_a_kind_of Spree::Product
assigns(:accessories).count.should be > 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment