33require "rails_helper"
44require "spree/testing_support/shared_examples/state_change_tracking"
55
6+ class AvailabilityTestValidator
7+ def validate ( line_item )
8+ if line_item . variant . sku == "UNAVAILABLE"
9+ line_item . errors . add ( :quantity , ":(" )
10+ false
11+ else
12+ true
13+ end
14+ end
15+ end
16+
17+ class EnsureUnitsTestValidator
18+ def validate ( line_item )
19+ if line_item . quantity != 1
20+ line_item . errors . add ( :quantity , ":(" )
21+ end
22+ end
23+ end
24+
25+ class TruthNumberGenerator
26+ def initialize ( options = { } )
27+ end
28+
29+ def generate
30+ "42"
31+ end
32+ end
33+
34+ class TestOrderMerger
35+ def initialize ( order )
36+ @order = order
37+ end
38+
39+ def merge! ( other_order , user = nil )
40+ [ @order , other_order , user ]
41+ end
42+ end
43+
644RSpec . describe Spree ::Order , type : :model do
745 let ( :store ) { create ( :store ) }
846 let ( :user ) { create ( :user , email : "solidus@example.com" ) }
410448
411449 describe "order_merger_class customization" do
412450 before do
413- class TestOrderMerger
414- def initialize ( order )
415- @order = order
416- end
417-
418- def merge! ( other_order , user = nil )
419- [ @order , other_order , user ]
420- end
421- end
422451 Spree ::Config . order_merger_class = TestOrderMerger
423452 end
424453
@@ -867,15 +896,6 @@ def call
867896 end
868897
869898 context "with order number generator configured" do
870- class TruthNumberGenerator
871- def initialize ( options = { } )
872- end
873-
874- def generate
875- "42"
876- end
877- end
878-
879899 before do
880900 expect ( Spree ::Config ) . to receive ( :order_number_generator ) do
881901 TruthNumberGenerator . new
@@ -1783,6 +1803,8 @@ def generate
17831803 # Class.new and define_method to avoid creating scope gates that would
17841804 # take this local variable out of scope.
17851805 inventory_unit = arbitrary_inventory_unit
1806+
1807+ # rubocop:disable Lint/ConstantDefinitionInBlock
17861808 TestInventoryUnitBuilder = Class . new do
17871809 def initialize ( order )
17881810 end
@@ -1791,6 +1813,7 @@ def initialize(order)
17911813 [ inventory_unit ]
17921814 }
17931815 end
1816+ # rubocop:enable Lint/ConstantDefinitionInBlock
17941817
17951818 test_stock_config = Spree ::Core ::StockConfiguration . new
17961819 test_stock_config . inventory_unit_builder_class = TestInventoryUnitBuilder . to_s
@@ -1831,23 +1854,15 @@ def initialize(order)
18311854 subject { order . send ( :ensure_inventory_units ) }
18321855
18331856 before do
1834- class TestValidator
1835- def validate ( line_item )
1836- if line_item . quantity != 1
1837- line_item . errors . add ( :quantity , ":(" )
1838- end
1839- end
1840- end
1841-
18421857 test_stock_config = Spree ::Core ::StockConfiguration . new
1843- test_stock_config . inventory_validator_class = TestValidator . to_s
1858+ test_stock_config . inventory_validator_class = EnsureUnitsTestValidator . to_s
18441859 stub_spree_preferences ( stock : test_stock_config )
18451860 end
18461861
18471862 let ( :order ) { create :order_with_line_items , line_items_count : 2 }
18481863
18491864 it "uses the configured validator" do
1850- expect_any_instance_of ( TestValidator ) . to receive ( :validate ) . twice . and_call_original
1865+ expect_any_instance_of ( EnsureUnitsTestValidator ) . to receive ( :validate ) . twice . and_call_original
18511866
18521867 subject
18531868 end
@@ -1873,19 +1888,8 @@ def validate(line_item)
18731888 subject { order . send ( :validate_line_item_availability ) }
18741889
18751890 before do
1876- class TestValidator
1877- def validate ( line_item )
1878- if line_item . variant . sku == "UNAVAILABLE"
1879- line_item . errors . add ( :quantity , ":(" )
1880- false
1881- else
1882- true
1883- end
1884- end
1885- end
1886-
18871891 test_stock_config = Spree ::Core ::StockConfiguration . new
1888- test_stock_config . availability_validator_class = TestValidator . to_s
1892+ test_stock_config . availability_validator_class = AvailabilityTestValidator . to_s
18891893 stub_spree_preferences ( stock : test_stock_config )
18901894 end
18911895
0 commit comments