Skip to content
This repository
Browse code

Making ordering common to all object per spec

  • Loading branch information...
commit 221594b6ab5a84af80be8021efc385d407e63788 1 parent 6743cd7
preethiramdev authored February 08, 2012
4  lib/rspec/mocks/message_expectation.rb
@@ -271,6 +271,10 @@ def generate_error
271 271
         end
272 272
       end
273 273
 
  274
+      def raise_out_of_order_error
  275
+        @error_generator.raise_out_of_order_error @sym
  276
+      end
  277
+
274 278
       # Constrains a stub or message expectation to invocations with specific
275 279
       # arguments.
276 280
       #
7  lib/rspec/mocks/order_group.rb
@@ -2,8 +2,7 @@ module RSpec
2 2
   module Mocks
3 3
     # @private
4 4
     class OrderGroup
5  
-      def initialize error_generator
6  
-        @error_generator = error_generator
  5
+      def initialize
7 6
         @ordering = Array.new
8 7
       end
9 8
 
@@ -14,7 +13,7 @@ def register(expectation)
14 13
 
15 14
       # @private
16 15
       def ready_for?(expectation)
17  
-        return @ordering.first == expectation
  16
+        @ordering.first == expectation
18 17
       end
19 18
 
20 19
       # @private
@@ -26,7 +25,7 @@ def consume
26 25
       def handle_order_constraint expectation
27 26
         return unless @ordering.include? expectation
28 27
         return consume if ready_for?(expectation)
29  
-        @error_generator.raise_out_of_order_error expectation.sym
  28
+        expectation.raise_out_of_order_error
30 29
       end
31 30
     end
32 31
   end
2  lib/rspec/mocks/proxy.rb
@@ -33,7 +33,7 @@ def initialize(object, name=nil, options={})
33 33
         @object = object
34 34
         @name = name
35 35
         @error_generator = ErrorGenerator.new object, name, options
36  
-        @expectation_ordering = OrderGroup.new @error_generator
  36
+        @expectation_ordering = RSpec::Mocks::space.expectation_ordering
37 37
           @messages_received = []
38 38
         @options = options
39 39
         @already_proxied_respond_to = false
8  lib/rspec/mocks/space.rb
@@ -17,13 +17,19 @@ def reset_all
17 17
           mock.rspec_reset
18 18
         end
19 19
         mocks.clear
  20
+        @expectation_ordering = nil
20 21
       end
21  
-      
  22
+
  23
+      def expectation_ordering
  24
+        @expectation_ordering ||= OrderGroup.new
  25
+      end
  26
+
22 27
     private
23 28
     
24 29
       def mocks
25 30
         @mocks ||= []
26 31
       end
  32
+
27 33
     end
28 34
   end
29 35
 end
33  spec/rspec/mocks/mock_ordering_spec.rb
@@ -8,7 +8,7 @@ module Mocks
8 8
       before do
9 9
         @double = double("test double")
10 10
       end
11  
-      
  11
+
12 12
       after do
13 13
         @double.rspec_reset
14 14
       end
@@ -21,6 +21,17 @@ module Mocks
21 21
         @double.rspec_verify
22 22
       end
23 23
 
  24
+      it "passes calls across objects in order" do
  25
+        another_double = double("another test double")
  26
+        @double.should_receive(:one).ordered
  27
+        another_double.should_receive(:two).ordered
  28
+        @double.should_receive(:two).ordered
  29
+        @double.one
  30
+        another_double.two
  31
+        @double.two
  32
+        @double.rspec_verify
  33
+      end
  34
+
24 35
       it "passes three calls in order" do
25 36
         @double.should_receive(:one).ordered
26 37
         @double.should_receive(:two).ordered
@@ -48,7 +59,19 @@ module Mocks
48 59
           @double.three
49 60
         end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :three out of order")
50 61
       end
51  
-      
  62
+
  63
+      it "fails if calls across objects are out of order" do
  64
+        another_double = double("another test double")
  65
+        @double.should_receive(:one).ordered
  66
+        another_double.should_receive(:two).ordered
  67
+        @double.should_receive(:three).ordered
  68
+        @double.one
  69
+        lambda do
  70
+          @double.three
  71
+        end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :three out of order")
  72
+        another_double.two
  73
+      end
  74
+
52 75
       it "fails if third call comes second" do
53 76
         @double.should_receive(:one).ordered
54 77
         @double.should_receive(:two).ordered
@@ -78,17 +101,17 @@ module Mocks
78 101
         @double.ignored_1
79 102
         @double.rspec_verify
80 103
       end
81  
-      
  104
+
82 105
       it "passes when duplicates exist" do
83 106
         @double.should_receive(:a).ordered
84 107
         @double.should_receive(:b).ordered
85 108
         @double.should_receive(:a).ordered
86  
-        
  109
+
87 110
         @double.a
88 111
         @double.b
89 112
         @double.a
90 113
       end
91  
-            
  114
+
92 115
     end
93 116
   end
94 117
 end

0 notes on commit 221594b

Please sign in to comment.
Something went wrong with that request. Please try again.