From 8643cbd8ce6389eeec899694ee29077e9303dd88 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 8 Aug 2020 16:58:17 +0100 Subject: [PATCH] Delete unused order.merge! and fix specs --- app/models/spree/order.rb | 29 +-- spec/models/spree/inventory_unit_spec.rb | 20 +- spec/models/spree/line_item_spec.rb | 20 +- spec/models/spree/order_contents_spec.rb | 38 +-- spec/models/spree/order_inventory_spec.rb | 86 +++---- spec/models/spree/order_spec.rb | 219 ++++++------------ .../models/spree/return_authorization_spec.rb | 21 +- 7 files changed, 143 insertions(+), 290 deletions(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index cc617cc0e1..7af9367193 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -399,11 +399,6 @@ module Spree find_line_item_by_variant(variant).present? end - def quantity_of(variant) - line_item = find_line_item_by_variant(variant) - line_item ? line_item.quantity : 0 - end - def find_line_item_by_variant(variant) line_items.detect { |line_item| line_item.variant_id == variant.id } end @@ -476,9 +471,9 @@ module Spree end def deliver_order_confirmation_email - if subscription.blank? - Delayed::Job.enqueue ConfirmOrderJob.new(id) - end + return if subscription.present? + + Delayed::Job.enqueue ConfirmOrderJob.new(id) end # Helper methods for checkout steps @@ -548,24 +543,6 @@ module Spree line_items.select(&:insufficient_stock?) end - def merge!(order) - order.line_items.each do |line_item| - next unless line_item.currency == currency - - current_line_item = line_items.find_by(variant: line_item.variant) - if current_line_item - current_line_item.quantity += line_item.quantity - current_line_item.save - else - line_item.order_id = id - line_item.save - end - end - # So that the destroy doesn't take out line items which may have been re-assigned - order.line_items.reload - order.destroy - end - def empty! line_items.destroy_all adjustments.destroy_all diff --git a/spec/models/spree/inventory_unit_spec.rb b/spec/models/spree/inventory_unit_spec.rb index 01019f7a1c..87088a6121 100644 --- a/spec/models/spree/inventory_unit_spec.rb +++ b/spec/models/spree/inventory_unit_spec.rb @@ -33,16 +33,7 @@ describe Spree::InventoryUnit do end it "finds inventory units from its stock location when the unit's variant matches the stock item's variant" do - Spree::InventoryUnit.backordered_for_stock_item(stock_item).should =~ [unit] - end - - it "does not find inventory units that aren't backordered" do - on_hand_unit = shipment.inventory_units.build - on_hand_unit.state = 'on_hand' - on_hand_unit.variant_id = 1 - on_hand_unit.save! - - Spree::InventoryUnit.backordered_for_stock_item(stock_item).should_not include(on_hand_unit) + expect(Spree::InventoryUnit.backordered_for_stock_item(stock_item)).to eq [unit] end it "does not find inventory units that don't match the stock item's variant" do @@ -51,7 +42,7 @@ describe Spree::InventoryUnit do other_variant_unit.variant = create(:variant) other_variant_unit.save! - Spree::InventoryUnit.backordered_for_stock_item(stock_item).should_not include(other_variant_unit) + expect(Spree::InventoryUnit.backordered_for_stock_item(stock_item)).to_not include(other_variant_unit) end end @@ -64,11 +55,6 @@ describe Spree::InventoryUnit do unit.variant.destroy expect(unit.reload.variant).to be_a Spree::Variant end - - it "can still fetch variants by eager loading (remove default_scope)" do - unit.variant.destroy - expect(Spree::InventoryUnit.joins(:variant).includes(:variant).first.variant).to be_a Spree::Variant - end end context "#finalize_units!" do @@ -83,7 +69,7 @@ describe Spree::InventoryUnit do it "should create a stock movement" do Spree::InventoryUnit.finalize_units!(inventory_units) - inventory_units.any?(&:pending).should be_false + expect(inventory_units.any?(&:pending)).to be_falsy end end end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 65d1924dd5..4307913a1d 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -41,9 +41,9 @@ module Spree line_item.currency = nil line_item.copy_price variant = line_item.variant - line_item.price.should == variant.price - line_item.cost_price.should == variant.cost_price - line_item.currency.should == variant.currency + expect(line_item.price).to eq variant.price + expect(line_item.cost_price).to eq variant.cost_price + expect(line_item.currency).to eq variant.currency end end @@ -52,7 +52,7 @@ module Spree it "copies over a variant's tax category" do line_item.tax_category = nil line_item.copy_tax_category - line_item.tax_category.should == line_item.variant.product.tax_category + expect(line_item.tax_category).to eq line_item.variant.product.tax_category end end @@ -69,14 +69,14 @@ module Spree end it "returns a Spree::Money representing the total for this line item" do - line_item.money.to_s.should == "$7.00" + expect(line_item.money.to_s).to eq "$7.00" end end describe '.single_money' do before { line_item.price = 3.50 } it "returns a Spree::Money representing the price for one variant" do - line_item.single_money.to_s.should == "$3.50" + expect(line_item.single_money.to_s).to eq "$3.50" end end @@ -98,7 +98,7 @@ module Spree line_item.target_shipment = order.shipments.first line_item.save - expect(line_item).to have(0).errors_on(:quantity) + expect(line_item.errors[:quantity]).to be_empty end it "doesnt allow to increase item quantity" do @@ -107,7 +107,7 @@ module Spree line_item.target_shipment = order.shipments.first line_item.save - expect(line_item).to have(1).errors_on(:quantity) + expect(line_item.errors[:quantity].first).to include "is out of stock" end end @@ -125,7 +125,7 @@ module Spree line_item.target_shipment = order.shipments.first line_item.save - expect(line_item).to have(0).errors_on(:quantity) + expect(line_item.errors[:quantity]).to be_empty end it "doesnt allow to increase quantity over stock availability" do @@ -134,7 +134,7 @@ module Spree line_item.target_shipment = order.shipments.first line_item.save - expect(line_item).to have(1).errors_on(:quantity) + expect(line_item.errors[:quantity].first).to include "is out of stock" end end end diff --git a/spec/models/spree/order_contents_spec.rb b/spec/models/spree/order_contents_spec.rb index df804df072..7a333521ee 100644 --- a/spec/models/spree/order_contents_spec.rb +++ b/spec/models/spree/order_contents_spec.rb @@ -12,32 +12,32 @@ describe Spree::OrderContents do context 'given quantity is not explicitly provided' do it 'should add one line item' do line_item = subject.add(variant) - line_item.quantity.should == 1 - order.line_items.size.should == 1 + expect(line_item.quantity).to eq 1 + expect(order.line_items.size).to eq 1 end end it 'should add line item if one does not exist' do line_item = subject.add(variant, 1) - line_item.quantity.should == 1 - order.line_items.size.should == 1 + expect(line_item.quantity).to eq 1 + expect(order.line_items.size).to eq 1 end it 'should update line item if one exists' do subject.add(variant, 1) line_item = subject.add(variant, 1) - line_item.quantity.should == 2 - order.line_items.size.should == 1 + expect(line_item.quantity).to eq 2 + expect(order.line_items.size).to eq 1 end it "should update order totals" do - order.item_total.to_f.should == 0.00 - order.total.to_f.should == 0.00 + expect(order.item_total.to_f).to eq 0.00 + expect(order.total.to_f).to eq 0.00 subject.add(variant, 1) - order.item_total.to_f.should == 19.99 - order.total.to_f.should == 19.99 + expect(order.item_total.to_f).to eq 19.99 + expect(order.total.to_f).to eq 19.99 end end @@ -57,7 +57,7 @@ describe Spree::OrderContents do line_item = subject.add(variant, 3) subject.remove(variant) - line_item.reload.quantity.should == 2 + expect(line_item.reload.quantity).to eq 2 end end @@ -65,28 +65,28 @@ describe Spree::OrderContents do line_item = subject.add(variant, 3) subject.remove(variant, 1) - line_item.reload.quantity.should == 2 + expect(line_item.reload.quantity).to eq 2 end it 'should remove line_item if quantity matches line_item quantity' do subject.add(variant, 1) subject.remove(variant, 1) - order.reload.find_line_item_by_variant(variant).should be_nil + expect(order.reload.find_line_item_by_variant(variant)).to be_nil end it "should update order totals" do - order.item_total.to_f.should == 0.00 - order.total.to_f.should == 0.00 + expect(order.item_total.to_f).to eq 0.00 + expect(order.total.to_f).to eq 0.00 subject.add(variant, 2) - order.item_total.to_f.should == 39.98 - order.total.to_f.should == 39.98 + expect(order.item_total.to_f).to eq 39.98 + expect(order.total.to_f).to eq 39.98 subject.remove(variant, 1) - order.item_total.to_f.should == 19.99 - order.total.to_f.should == 19.99 + expect(order.item_total.to_f).to eq 19.99 + expect(order.total.to_f).to eq 19.99 end end end diff --git a/spec/models/spree/order_inventory_spec.rb b/spec/models/spree/order_inventory_spec.rb index 2a1c3baa18..39f48aed0e 100644 --- a/spec/models/spree/order_inventory_spec.rb +++ b/spec/models/spree/order_inventory_spec.rb @@ -9,7 +9,7 @@ describe Spree::OrderInventory do it 'inventory_units_for should return array of units for a given variant' do units = subject.inventory_units_for(line_item.variant) - units.map(&:variant_id).should == [line_item.variant.id] + expect(units.map(&:variant_id)).to eq [line_item.variant.id] end context "when order is missing inventory units" do @@ -18,13 +18,13 @@ describe Spree::OrderInventory do end it 'should be a messed up order' do - order.shipments.first.inventory_units_for(line_item.variant).size.should == 1 - line_item.reload.quantity.should == 2 + expect(order.shipments.first.inventory_units_for(line_item.variant).size).to eq 1 + expect(line_item.reload.quantity).to eq 2 end it 'should increase the number of inventory units' do subject.verify(line_item) - order.reload.shipments.first.inventory_units_for(line_item.variant).size.should == 2 + expect(order.reload.shipments.first.inventory_units_for(line_item.variant).size).to eq 2 end end @@ -37,58 +37,27 @@ describe Spree::OrderInventory do it "doesn't unstock items" do shipment.stock_location.should_not_receive(:unstock) - subject.send(:add_to_shipment, shipment, variant, 5).should == 5 + expect(subject.send(:add_to_shipment, shipment, variant, 5)).to eq 5 end end it 'should create inventory_units in the necessary states' do shipment.stock_location.should_receive(:fill_status).with(variant, 5).and_return([3, 2]) - subject.send(:add_to_shipment, shipment, variant, 5).should == 5 + expect(subject.send(:add_to_shipment, shipment, variant, 5)).to eq 5 units = shipment.inventory_units.group_by &:variant_id units = units[variant.id].group_by &:state - units['backordered'].size.should == 2 - units['on_hand'].size.should == 3 + expect(units['backordered'].size).to eq 2 + expect(units['on_hand'].size).to eq 3 end it 'should create stock_movement' do - subject.send(:add_to_shipment, shipment, variant, 5).should == 5 + expect(subject.send(:add_to_shipment, shipment, variant, 5)).to eq 5 stock_item = shipment.stock_location.stock_item(variant) movement = stock_item.stock_movements.last - # movement.originator.should == shipment - movement.quantity.should == -5 - end - end - - context "#determine_target_shipment" do - let(:stock_location) { create :stock_location } - let(:variant) { line_item.variant } - - before do - order.shipments.create(stock_location_id: stock_location.id) - shipped = order.shipments.create(stock_location_id: order.shipments.first.stock_location.id) - shipped.update_column(:state, 'shipped') - end - - it 'should select first non-shipped shipment that already contains given variant' do - shipment = subject.send(:determine_target_shipment, variant) - shipment.shipped?.should be_false - shipment.inventory_units_for(variant).should_not be_empty - variant.stock_location_ids.include?(shipment.stock_location_id).should be_true - end - - context "when no shipments already contain this varint" do - it 'selects first non-shipped shipment that leaves from same stock_location' do - subject.send(:remove_from_shipment, order.shipments.first, variant, line_item.quantity) - - shipment = subject.send(:determine_target_shipment, variant) - shipment.reload - shipment.shipped?.should be_false - shipment.inventory_units_for(variant).should be_empty - variant.stock_location_ids.include?(shipment.stock_location_id).should be_true - end + expect(movement.quantity).to eq(-5) end end @@ -102,13 +71,13 @@ describe Spree::OrderInventory do end it 'should be a messed up order' do - order.shipments.first.inventory_units_for(line_item.variant).size.should == 3 - line_item.quantity.should == 2 + expect(order.shipments.first.inventory_units_for(line_item.variant).size).to eq 3 + expect(line_item.quantity).to eq 2 end it 'should decrease the number of inventory units' do subject.verify(line_item) - order.reload.shipments.first.inventory_units_for(line_item.variant).size.should == 2 + expect(order.reload.shipments.first.inventory_units_for(line_item.variant).size).to eq 2 end context '#remove_from_shipment' do @@ -120,56 +89,55 @@ describe Spree::OrderInventory do it "doesn't restock items" do shipment.stock_location.should_not_receive(:restock) - subject.send(:remove_from_shipment, shipment, variant, 1).should == 1 + expect(subject.send(:remove_from_shipment, shipment, variant, 1)).to eq 1 end end it 'should create stock_movement' do - subject.send(:remove_from_shipment, shipment, variant, 1).should == 1 + expect(subject.send(:remove_from_shipment, shipment, variant, 1)).to eq 1 stock_item = shipment.stock_location.stock_item(variant) movement = stock_item.stock_movements.last - # movement.originator.should == shipment - movement.quantity.should == 1 + expect(movement.quantity).to eq 1 end it 'should destroy backordered units first' do - shipment.stub(inventory_units_for: [mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'backordered'), - mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'on_hand'), - mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'backordered')]) + shipment.stub(inventory_units_for: [build(:inventory_unit, variant_id: variant.id, state: 'backordered'), + build(:inventory_unit, variant_id: variant.id, state: 'on_hand'), + build(:inventory_unit, variant_id: variant.id, state: 'backordered')]) shipment.inventory_units_for[0].should_receive(:destroy) shipment.inventory_units_for[1].should_not_receive(:destroy) shipment.inventory_units_for[2].should_receive(:destroy) - subject.send(:remove_from_shipment, shipment, variant, 2).should == 2 + expect(subject.send(:remove_from_shipment, shipment, variant, 2)).to eq 2 end it 'should destroy unshipped units first' do - shipment.stub(inventory_units_for: [mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'shipped'), - mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'on_hand')] ) + shipment.stub(inventory_units_for: [build(:inventory_unit, variant_id: variant.id, state: 'shipped'), + build(:inventory_unit, variant_id: variant.id, state: 'on_hand')] ) shipment.inventory_units_for[0].should_not_receive(:destroy) shipment.inventory_units_for[1].should_receive(:destroy) - subject.send(:remove_from_shipment, shipment, variant, 1).should == 1 + expect(subject.send(:remove_from_shipment, shipment, variant, 1)).to eq 1 end it 'only attempts to destroy as many units as are eligible, and return amount destroyed' do - shipment.stub(inventory_units_for: [mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'shipped'), - mock_model(Spree::InventoryUnit, variant_id: variant.id, state: 'on_hand')] ) + shipment.stub(inventory_units_for: [build(:inventory_unit, variant_id: variant.id, state: 'shipped'), + build(:inventory_unit, variant_id: variant.id, state: 'on_hand')] ) shipment.inventory_units_for[0].should_not_receive(:destroy) shipment.inventory_units_for[1].should_receive(:destroy) - subject.send(:remove_from_shipment, shipment, variant, 1).should == 1 + expect(subject.send(:remove_from_shipment, shipment, variant, 1)).to eq 1 end it 'should destroy self if not inventory units remain' do shipment.inventory_units.stub(count: 0) shipment.should_receive(:destroy) - subject.send(:remove_from_shipment, shipment, variant, 1).should == 1 + expect(subject.send(:remove_from_shipment, shipment, variant, 1)).to eq 1 end end end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 3dcebe191b..eccf529c92 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -3,47 +3,34 @@ require 'spec_helper' describe Spree::Order do include OpenFoodNetwork::EmailHelper - let(:user) { stub_model(Spree::LegacyUser, email: "spree@example.com") } - let(:order) { stub_model(Spree::Order, user: user) } + let(:user) { build(:user, email: "spree@example.com") } + let(:order) { build(:order, user: user) } before do - Spree::LegacyUser.stub(current: mock_model(Spree::LegacyUser, id: 123)) + Spree::LegacyUser.stub(current: build(:user, id: 123)) end context "#products" do - before :each do - @variant1 = mock_model(Spree::Variant, product: "product1") - @variant2 = mock_model(Spree::Variant, product: "product2") - @line_items = [mock_model(Spree::LineItem, product: "product1", variant: @variant1, variant_id: @variant1.id, quantity: 1), - mock_model(Spree::LineItem, product: "product2", variant: @variant2, variant_id: @variant2.id, quantity: 2)] - order.stub(line_items: @line_items) - end + let(:order) { create(:order_with_line_items) } it "should return ordered products" do - order.products.should == ['product1', 'product2'] + expect(order.products.first).to eq order.line_items.first.product end it "contains?" do - order.contains?(@variant1).should be_true - end - - it "gets the quantity of a given variant" do - order.quantity_of(@variant1).should == 1 - - @variant3 = mock_model(Spree::Variant, product: "product3") - order.quantity_of(@variant3).should == 0 + expect(order.contains?(order.line_items.first.variant)).to be_truthy end it "can find a line item matching a given variant" do - order.find_line_item_by_variant(@variant1).should_not be_nil - order.find_line_item_by_variant(mock_model(Spree::Variant)).should be_nil + expect(order.find_line_item_by_variant(order.line_items.third.variant)).to_not be_nil + expect(order.find_line_item_by_variant(build(:variant))).to be_nil end end context "#generate_order_number" do it "should generate a random string" do - order.generate_order_number.is_a?(String).should be_true - (!order.generate_order_number.to_s.empty?).should be_true + expect(order.generate_order_number.is_a?(String)).to be_truthy + expect((!order.generate_order_number.to_s.empty?)).to be_truthy end end @@ -55,15 +42,15 @@ describe Spree::Order do order.user = nil order.email = nil order.associate_user!(user) - order.user.should == user - order.email.should == user.email - order.created_by.should == user + expect(order.user).to eq user + expect(order.email).to eq user.email + expect(order.created_by).to eq user # verify that the changes we made were persisted order.reload - order.user.should == user - order.email.should == user.email - order.created_by.should == user + expect(order.user).to eq user + expect(order.email).to eq user.email + expect(order.created_by).to eq user end it "should not overwrite the created_by if it already is set" do @@ -74,15 +61,15 @@ describe Spree::Order do order.user = nil order.email = nil order.associate_user!(user) - order.user.should == user - order.email.should == user.email - order.created_by.should == creator + expect(order.user).to eq user + expect(order.email).to eq user.email + expect(order.created_by).to eq creator # verify that the changes we made were persisted order.reload - order.user.should == user - order.email.should == user.email - order.created_by.should == creator + expect(order.user).to eq user + expect(order.email).to eq user.email + expect(order.created_by).to eq creator end it "should associate a user with a non-persisted order" do @@ -107,7 +94,7 @@ describe Spree::Order do context "#create" do it "should assign an order number" do order = Spree::Order.create - order.number.should_not be_nil + expect(order.number).to_not be_nil end end @@ -116,27 +103,27 @@ describe Spree::Order do it "should be true for order in the 'complete' state" do order.stub(complete?: true) - order.can_ship?.should be_true + expect(order.can_ship?).to be_truthy end it "should be true for order in the 'resumed' state" do order.stub(resumed?: true) - order.can_ship?.should be_true + expect(order.can_ship?).to be_truthy end it "should be true for an order in the 'awaiting return' state" do order.stub(awaiting_return?: true) - order.can_ship?.should be_true + expect(order.can_ship?).to be_truthy end it "should be true for an order in the 'returned' state" do order.stub(returned?: true) - order.can_ship?.should be_true + expect(order.can_ship?).to be_truthy end it "should be false if the order is neither in the 'complete' nor 'resumed' state" do order.stub(resumed?: false, complete?: false) - order.can_ship?.should be_false + expect(order.can_ship?).to be_falsy end end @@ -181,7 +168,7 @@ describe Spree::Order do order.stub(paid?: true, complete?: true) order.finalize! order.reload # reload so we're sure the changes are persisted - order.shipment_state.should == 'ready' + expect(order.shipment_state).to eq 'ready' end after { Spree::Config.set track_inventory_levels: true } @@ -192,15 +179,9 @@ describe Spree::Order do end it "should send an order confirmation email" do - mail_message = double "Mail::Message" - Spree::OrderMailer.should_receive(:confirm_email).with(order.id).and_return mail_message - mail_message.should_receive :deliver - order.finalize! - end - - it "should continue even if confirmation email delivery fails" do - Spree::OrderMailer.should_receive(:confirm_email).with(order.id).and_raise 'send failed!' - order.finalize! + expect do + order.finalize! + end.to enqueue_job ConfirmOrderJob end it "should freeze all adjustments" do @@ -226,17 +207,17 @@ describe Spree::Order do end context "#process_payments!" do - let(:payment) { stub_model(Spree::Payment) } + let(:payment) { build(:payment) } before { order.stub pending_payments: [payment], total: 10 } it "should process the payments" do payment.should_receive(:process!) - order.process_payments!.should be_true + expect(order.process_payments!).to be_truthy end it "should return false if no pending_payments available" do order.stub pending_payments: [] - order.process_payments!.should be_false + expect(order.process_payments!).to be_falsy end context "when a payment raises a GatewayError" do @@ -244,12 +225,12 @@ describe Spree::Order do it "should return true when configured to allow checkout on gateway failures" do Spree::Config.set allow_checkout_on_gateway_error: true - order.process_payments!.should be_true + expect(order.process_payments!).to be_truthy end it "should return false when not configured to allow checkout on gateway failures" do Spree::Config.set allow_checkout_on_gateway_error: false - order.process_payments!.should be_false + expect(order.process_payments!).to be_falsy end end end @@ -258,12 +239,12 @@ describe Spree::Order do it "should return positive amount when payment_total is less than total" do order.payment_total = 20.20 order.total = 30.30 - order.outstanding_balance.should == 10.10 + expect(order.outstanding_balance).to eq 10.10 end it "should return negative amount when payment_total is greater than total" do order.total = 8.20 order.payment_total = 10.20 - order.outstanding_balance.should be_within(0.001).of(-2.00) + expect(order.outstanding_balance).to be_within(0.001).of(-2.00) end end @@ -271,44 +252,38 @@ describe Spree::Order do it "should be true when total greater than payment_total" do order.total = 10.10 order.payment_total = 9.50 - order.outstanding_balance?.should be_true + expect(order.outstanding_balance?).to be_truthy end it "should be true when total less than payment_total" do order.total = 8.25 order.payment_total = 10.44 - order.outstanding_balance?.should be_true + expect(order.outstanding_balance?).to be_truthy end it "should be false when total equals payment_total" do order.total = 10.10 order.payment_total = 10.10 - order.outstanding_balance?.should be_false + expect(order.outstanding_balance?).to be_falsy end end context "#completed?" do it "should indicate if order is completed" do order.completed_at = nil - order.completed?.should be_false + expect(order.completed?).to be_falsy order.completed_at = Time.zone.now - order.completed?.should be_true + expect(order.completed?).to be_truthy end end - it 'is backordered if one of the shipments is backordered' do - order.stub(shipments: [mock_model(Spree::Shipment, backordered?: false), - mock_model(Spree::Shipment, backordered?: true)]) - order.should be_backordered - end - context "#allow_checkout?" do it "should be true if there are line_items in the order" do order.stub_chain(:line_items, count: 1) - order.checkout_allowed?.should be_true + expect(order.checkout_allowed?).to be_truthy end it "should be false if there are no line_items in the order" do order.stub_chain(:line_items, count: 0) - order.checkout_allowed?.should be_false + expect(order.checkout_allowed?).to be_falsy end end @@ -318,7 +293,7 @@ describe Spree::Order do @order.line_items = [create(:line_item, quantity: 2), create(:line_item, quantity: 1)] end it "should return the correct number of items" do - @order.item_count.should == 3 + expect(@order.item_count).to eq 3 end end @@ -329,7 +304,7 @@ describe Spree::Order do create(:line_item, price: 1.0, quantity: 1)] end it "should return the correct lum sum of items" do - @order.amount.should == 3.0 + expect(@order.amount).to eq 3.0 end end @@ -338,31 +313,34 @@ describe Spree::Order do order.state = 'canceled' order.shipment_state = 'ready' order.completed_at = Time.zone.now - order.can_cancel?.should be_false + expect(order.can_cancel?).to be_falsy end it "should be true for completed order with no shipment" do order.state = 'complete' order.shipment_state = nil order.completed_at = Time.zone.now - order.can_cancel?.should be_true + expect(order.can_cancel?).to be_truthy end end context "insufficient_stock_lines" do - let(:line_item) { mock_model Spree::LineItem, insufficient_stock?: true } + let(:line_item) { build(:line_item) } - before { order.stub(line_items: [line_item]) } + before do + order.stub(line_items: [line_item]) + allow(line_item).to receive(:insufficient_stock?) { true } + end it "should return line_item that has insufficient stock on hand" do - order.insufficient_stock_lines.size.should == 1 - order.insufficient_stock_lines.include?(line_item).should be_true + expect(order.insufficient_stock_lines.size).to eq 1 + expect(order.insufficient_stock_lines.include?(line_item)).to be_truthy end end context "empty!" do it "should clear out all line items and adjustments" do - order = stub_model(Spree::Order) + order = build(:order) order.stub(line_items: line_items = []) order.stub(adjustments: adjustments = []) order.line_items.should_receive(:destroy_all) @@ -375,28 +353,28 @@ describe Spree::Order do context "#display_outstanding_balance" do it "returns the value as a spree money" do order.stub(:outstanding_balance) { 10.55 } - order.display_outstanding_balance.should == Spree::Money.new(10.55) + expect(order.display_outstanding_balance).to eq Spree::Money.new(10.55) end end context "#display_item_total" do it "returns the value as a spree money" do order.stub(:item_total) { 10.55 } - order.display_item_total.should == Spree::Money.new(10.55) + expect(order.display_item_total).to eq Spree::Money.new(10.55) end end context "#display_adjustment_total" do it "returns the value as a spree money" do order.adjustment_total = 10.55 - order.display_adjustment_total.should == Spree::Money.new(10.55) + expect(order.display_adjustment_total).to eq Spree::Money.new(10.55) end end context "#display_total" do it "returns the value as a spree money" do order.total = 10.55 - order.display_total.should == Spree::Money.new(10.55) + expect(order.display_total).to eq Spree::Money.new(10.55) end end @@ -405,7 +383,7 @@ describe Spree::Order do before { order.currency = "ABC" } it "returns the currency from the object" do - order.currency.should == "ABC" + expect(order.currency).to eq "ABC" end end @@ -413,66 +391,11 @@ describe Spree::Order do before { order.currency = nil } it "returns the globally configured currency" do - order.currency.should == "USD" + expect(order.currency).to eq Spree::Config[:currency] end end end - # Regression tests for Spree #2179 - context "#merge!" do - let(:variant) { create(:variant) } - let(:order_1) { Spree::Order.create } - let(:order_2) { Spree::Order.create } - - it "destroys the other order" do - order_1.merge!(order_2) - lambda { order_2.reload }.should raise_error(ActiveRecord::RecordNotFound) - end - - context "merging together two orders with line items for the same variant" do - before do - order_1.contents.add(variant, 1) - order_2.contents.add(variant, 1) - end - - specify do - order_1.merge!(order_2) - order_1.line_items.count.should == 1 - - line_item = order_1.line_items.first - line_item.quantity.should == 2 - line_item.variant_id.should == variant.id - end - end - - context "merging together two orders with different line items" do - let(:variant_2) { create(:variant) } - - before do - order_1.contents.add(variant, 1) - order_2.contents.add(variant_2, 1) - end - - specify do - order_1.merge!(order_2) - line_items = order_1.line_items - line_items.count.should == 2 - - # No guarantee on ordering of line items, so we do this: - line_items.pluck(:quantity).should =~ [1, 1] - line_items.pluck(:variant_id).should =~ [variant.id, variant_2.id] - end - end - end - - context "#confirmation_required?" do - it "does not bomb out when an order has an unpersisted payment" do - order = Spree::Order.new - order.payments.build - assert !order.confirmation_required? - end - end - # Regression test for Spree #2191 context "when an order has an adjustment that zeroes the total, but another adjustment for shipping that raises it above zero" do let!(:persisted_order) { create(:order) } @@ -496,7 +419,7 @@ describe Spree::Order do it "transitions from delivery to payment" do persisted_order.stub(payment_required?: true) persisted_order.next! - persisted_order.state.should == "payment" + expect(persisted_order.state).to eq "payment" end end @@ -504,12 +427,12 @@ describe Spree::Order do let(:order) { Spree::Order.new } context "total is zero" do - it { order.payment_required?.should be_false } + it { expect(order.payment_required?).to be_falsy } end context "total > zero" do before { order.stub(total: 1) } - it { order.payment_required?.should be_true } + it { expect(order.payment_required?).to be_truthy } end end @@ -559,7 +482,7 @@ describe Spree::Order do let(:tax_using_ship_address) { true } it 'returns ship_address' do - subject.should == order.ship_address + expect(subject).to eq order.ship_address end end @@ -567,7 +490,7 @@ describe Spree::Order do let(:tax_using_ship_address) { false } it "returns bill_address" do - subject.should == order.bill_address + expect(subject).to eq order.bill_address end end end @@ -586,11 +509,11 @@ describe Spree::Order do end it 'returns an order_updater_decorator class' do - order.updater.class.should == FakeOrderUpdaterDecorator + expect(order.updater.class).to eq FakeOrderUpdaterDecorator end it 'decorates a Spree::OrderUpdater' do - order.updater.decorated_object.class.should == Spree::OrderUpdater + expect(order.updater.decorated_object.class).to eq Spree::OrderUpdater end end diff --git a/spec/models/spree/return_authorization_spec.rb b/spec/models/spree/return_authorization_spec.rb index c57f6b4d8b..893965850a 100644 --- a/spec/models/spree/return_authorization_spec.rb +++ b/spec/models/spree/return_authorization_spec.rb @@ -3,16 +3,15 @@ require 'spec_helper' describe Spree::ReturnAuthorization do - let(:stock_location) { Spree::StockLocation.create(name: "test") } let(:order) { FactoryGirl.create(:shipped_order) } let(:variant) { order.shipments.first.inventory_units.first.variant } - let(:return_authorization) { Spree::ReturnAuthorization.new(order: order, stock_location_id: stock_location.id) } + let(:return_authorization) { Spree::ReturnAuthorization.new(order: order) } context "save" do it "should be invalid when order has no inventory units" do order.shipments.destroy_all return_authorization.save - return_authorization.errors[:order].should == ["has no shipped units"] + expect(return_authorization.errors[:order]).to eq ["has no shipped units"] end it "should generate RMA number" do @@ -25,7 +24,7 @@ describe Spree::ReturnAuthorization do context "on empty rma" do it "should associate inventory unit" do return_authorization.add_variant(variant.id, 1) - return_authorization.inventory_units.size.should == 1 + expect(return_authorization.inventory_units.size).to eq 1 end it "should associate inventory units as shipped" do @@ -58,12 +57,12 @@ describe Spree::ReturnAuthorization do context "can_receive?" do it "should allow_receive when inventory units assigned" do return_authorization.stub(inventory_units: [1, 2, 3]) - return_authorization.can_receive?.should be_true + expect(return_authorization.can_receive?).to be_truthy end it "should not allow_receive with no inventory units" do return_authorization.stub(inventory_units: []) - return_authorization.can_receive?.should be_false + expect(return_authorization.can_receive?).to be_falsy end end @@ -101,7 +100,7 @@ describe Spree::ReturnAuthorization do it "should ensure the amount is always positive" do return_authorization.amount = -10 return_authorization.send :force_positive_amount - return_authorization.amount.should == 10 + expect(return_authorization.amount).to eq 10 end end @@ -115,24 +114,24 @@ describe Spree::ReturnAuthorization do context "currency" do before { order.stub(:currency) { "ABC" } } it "returns the order currency" do - return_authorization.currency.should == "ABC" + expect(return_authorization.currency).to eq "ABC" end end context "display_amount" do it "returns a Spree::Money" do return_authorization.amount = 21.22 - return_authorization.display_amount.should == Spree::Money.new(21.22) + expect(return_authorization.display_amount).to eq Spree::Money.new(21.22) end end context "returnable_inventory" do pending "should return inventory from shipped shipments" do - return_authorization.returnable_inventory.should == [inventory_unit] + expect(return_authorization.returnable_inventory).to eq [inventory_unit] end pending "should not return inventory from unshipped shipments" do - return_authorization.returnable_inventory.should == [] + expect(return_authorization.returnable_inventory).to eq [] end end end