Skip to content
Browse files

Making ordering common to all object per spec

  • Loading branch information...
1 parent 6743cd7 commit 221594b6ab5a84af80be8021efc385d407e63788 @preethiramdev committed Feb 8, 2012
View
4 lib/rspec/mocks/message_expectation.rb
@@ -271,6 +271,10 @@ def generate_error
end
end
+ def raise_out_of_order_error
+ @error_generator.raise_out_of_order_error @sym
+ end
+
# Constrains a stub or message expectation to invocations with specific
# arguments.
#
View
7 lib/rspec/mocks/order_group.rb
@@ -2,8 +2,7 @@ module RSpec
module Mocks
# @private
class OrderGroup
- def initialize error_generator
- @error_generator = error_generator
+ def initialize
@ordering = Array.new
end
@@ -14,7 +13,7 @@ def register(expectation)
# @private
def ready_for?(expectation)
- return @ordering.first == expectation
+ @ordering.first == expectation
end
# @private
@@ -26,7 +25,7 @@ def consume
def handle_order_constraint expectation
return unless @ordering.include? expectation
return consume if ready_for?(expectation)
- @error_generator.raise_out_of_order_error expectation.sym
+ expectation.raise_out_of_order_error
end
end
end
View
2 lib/rspec/mocks/proxy.rb
@@ -33,7 +33,7 @@ def initialize(object, name=nil, options={})
@object = object
@name = name
@error_generator = ErrorGenerator.new object, name, options
- @expectation_ordering = OrderGroup.new @error_generator
+ @expectation_ordering = RSpec::Mocks::space.expectation_ordering
@messages_received = []
@options = options
@already_proxied_respond_to = false
View
8 lib/rspec/mocks/space.rb
@@ -17,13 +17,19 @@ def reset_all
mock.rspec_reset
end
mocks.clear
+ @expectation_ordering = nil
end
-
+
+ def expectation_ordering
+ @expectation_ordering ||= OrderGroup.new
+ end
+
private
def mocks
@mocks ||= []
end
+
end
end
end
View
33 spec/rspec/mocks/mock_ordering_spec.rb
@@ -8,7 +8,7 @@ module Mocks
before do
@double = double("test double")
end
-
+
after do
@double.rspec_reset
end
@@ -21,6 +21,17 @@ module Mocks
@double.rspec_verify
end
+ it "passes calls across objects in order" do
+ another_double = double("another test double")
+ @double.should_receive(:one).ordered
+ another_double.should_receive(:two).ordered
+ @double.should_receive(:two).ordered
+ @double.one
+ another_double.two
+ @double.two
+ @double.rspec_verify
+ end
+
it "passes three calls in order" do
@double.should_receive(:one).ordered
@double.should_receive(:two).ordered
@@ -48,7 +59,19 @@ module Mocks
@double.three
end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :three out of order")
end
-
+
+ it "fails if calls across objects are out of order" do
+ another_double = double("another test double")
+ @double.should_receive(:one).ordered
+ another_double.should_receive(:two).ordered
+ @double.should_receive(:three).ordered
+ @double.one
+ lambda do
+ @double.three
+ end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :three out of order")
+ another_double.two
+ end
+
it "fails if third call comes second" do
@double.should_receive(:one).ordered
@double.should_receive(:two).ordered
@@ -78,17 +101,17 @@ module Mocks
@double.ignored_1
@double.rspec_verify
end
-
+
it "passes when duplicates exist" do
@double.should_receive(:a).ordered
@double.should_receive(:b).ordered
@double.should_receive(:a).ordered
-
+
@double.a
@double.b
@double.a
end
-
+
end
end
end

0 comments on commit 221594b

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