Fix checkout totals, also amend factories for valid test shipping method data

This commit is contained in:
Rohan Mitchell
2012-09-21 12:35:01 +10:00
parent 593f3887c8
commit a1b5102476
5 changed files with 39 additions and 10 deletions

View File

@@ -47,6 +47,8 @@ Spree::Order.class_eval do
if self.shipping_method
self.save!
self.create_shipment!
else
raise 'No default shipping method found'
end
end

View File

@@ -6,15 +6,15 @@
%h5
Item total
\:
%span.order-total= order_subtotal(@order)
%span.order-total.item-total= number_to_currency @order.item_total
%h5
Shipping total
\:
%span.order-total= order_delivery_fee_subtotal(@order)
%span.order-total.shipping-total= order_delivery_fee_subtotal(@order)
%h4
Total
\:
%span.order-total= number_to_currency(order_subtotal(@order, :format_as_currency => false) + order_delivery_fee_subtotal(@order, :format_as_currency => false))
%span.order-total.grand-total= order_subtotal(@order)
.links{'data-hook' => "cart_buttons"}
= button_tag :class => 'primary', :id => 'update-button' do

View File

@@ -4,9 +4,11 @@ require 'spree/core/current_order'
describe Spree::DistributorsController do
include Spree::Core::CurrentOrder
before do
before :each do
stub!(:before_save_new_order)
stub!(:after_save_new_order)
create(:itemwise_shipping_method)
end

View File

@@ -24,7 +24,7 @@ FactoryGirl.define do
factory :product_distribution, :class => Spree::ProductDistribution do
product { |pd| Spree::Product.first || FactoryGirl.create(:product) }
distributor { |pd| Spree::Distributor.first || FactoryGirl.create(:distributor) }
shipping_method { |pd| Spree::ShippingMethod.first || FactoryGirl.create(:shipping_method) }
shipping_method { |pd| Spree::ShippingMethod.where("name != 'Delivery'").first || FactoryGirl.create(:shipping_method) }
end
factory :itemwise_shipping_method, :parent => :shipping_method do
@@ -50,12 +50,25 @@ FactoryGirl.modify do
# before(:create) do |product, evaluator|
# product.product_distributions = [FactoryGirl.create(:product_distribution, :product => product)]
# end
# Do not create products distributed via the 'Delivery' shipping method
after(:create) do |product, evaluator|
pd = product.product_distributions.first
if pd.andand.shipping_method.andand.name == 'Delivery'
pd.shipping_method = Spree::ShippingMethod.where("name != 'Delivery'").first || FactoryGirl.create(:shipping_method)
pd.save!
end
end
end
factory :line_item do
shipping_method { |li| li.product.shipping_method_for_distributor(li.order.distributor) }
end
factory :shipping_method do
display_on ''
end
factory :address do
state { Spree::State.find_by_name 'Victoria' }
country { Spree::Country.find_by_name 'Australia' || Spree::Country.first }

View File

@@ -24,26 +24,38 @@ feature %q{
# And the product should not have been added to my cart
Spree::Order.last.should be_nil
#order.line_items.should be_empty
end
scenario "adding the first product to the cart" do
# Given a product and some distributors
create(:itemwise_shipping_method)
# Given a product, some distributors and a defined shipping cost
d1 = create(:distributor)
d2 = create(:distributor)
p = create(:product, :distributors => [d1])
create(:product, :distributors => [d2])
p = create(:product, :price => 12.34)
create(:product_distribution, :product => p, :distributor => d1, :shipping_method => create(:shipping_method))
# ... with a flat rate shipping method of cost $1.23
sm = p.product_distributions.first.shipping_method
sm.calculator.preferred_amount = 1.23
sm.calculator.save!
# When I choose a distributor
visit spree.root_path
click_link d2.name
# When I add an item to my cart from a different distributor
# And I add an item to my cart from a different distributor
visit spree.product_path p
select d1.name, :from => 'distributor_id'
click_button 'Add To Cart'
# Then the item should be in my cart, with shipping method set for the line item
# Then the correct totals should be displayed
page.should have_selector 'span.item-total', :text => '$12.34'
page.should have_selector 'span.shipping-total', :text => '$1.23'
page.should have_selector 'span.grand-total', :text => '$13.57'
# And the item should be in my cart, with shipping method set for the line item
order = Spree::Order.last
line_item = order.line_items.first
line_item.product.should == p