Permalink
Browse files

Correct issue with per item calculator when used with shipping method

Relates to #1596

Merges #1601
  • Loading branch information...
1 parent bc88123 commit 3e626c5bdb04bb843540e22c64c6b06a6c6390dd @laurens laurens committed with radar May 25, 2012
Showing with 23 additions and 11 deletions.
  1. +1 −1 core/app/models/spree/calculator/per_item.rb
  2. +22 −10 core/spec/models/calculator/per_item_spec.rb
@@ -11,7 +11,7 @@ def self.description
def compute(object=nil)
return 0 if object.nil?
self.preferred_amount * object.line_items.reduce(0) do |sum, value|
- if matching_products && matching_products.include?(value.product)
+ if !matching_products || matching_products.include?(value.product)
value_to_add = value.quantity
else
value_to_add = 0
@@ -2,24 +2,36 @@
describe Spree::Calculator::PerItem do
# Like an order object, but not quite...
- let!(:product) { double("Product") }
- let!(:line_items) { [double("LineItem", :quantity => 5, :product => product)] * 3 }
+ let!(:product1) { double("Product") }
+ let!(:product2) { double("Product") }
+ let!(:line_items) { [double("LineItem", :quantity => 5, :product => product1), double("LineItem", :quantity => 3, :product => product2)] }
let!(:object) { double("Order", :line_items => line_items) }
- let!(:calculable) { double("Calculable", :promotion => promotion) }
- let!(:promotion) { double("Promotion", :rules => [double("Rule", :products => [product])]) }
+
+ let!(:shipping_calculable) { double("Calculable") }
+ let!(:promotion_calculable) { double("Calculable", :promotion => promotion) }
+
+ let!(:promotion) { double("Promotion", :rules => [double("Rule", :products => [product1])]) }
- let!(:calculator) do
- calculator = Spree::Calculator::PerItem.new(:preferred_amount => 10)
- calculator.stub(:calculable => calculable)
- calculator
- end
+ let!(:calculator) { Spree::Calculator::PerItem.new(:preferred_amount => 10) }
# regression test for #1414
it "correctly calculates per item shipping" do
- calculator.compute(object).to_f.should == 150 # 5 x 3 x 10
+ calculator.stub(:calculable => shipping_calculable)
+ calculator.compute(object).to_f.should == 80 # 5 x 10 + 3 x 10
+ end
+
+ it "correctly calculates per item promotion" do
+ calculator.stub(:calculable => promotion_calculable)
+ calculator.compute(object).to_f.should == 50 # 5 x 10
+ end
+
+ it "returns 0 when no object passed" do
+ calculator.stub(:calculable => shipping_calculable)
+ calculator.compute.should == 0
end
it "returns 0 when no object passed" do
+ calculator.stub(:calculable => promotion_calculable)
calculator.compute.should == 0
end

0 comments on commit 3e626c5

Please sign in to comment.