Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Issue 101 #106

Merged
merged 2 commits into from

2 participants

@preethiramdev

Solution for #101: making the ordering common to all objects in the spec instead of specific to each object.

@dchelimsky dchelimsky commented on the diff
spec/spec_helper.rb
@@ -36,7 +36,7 @@ def treats_method_missing_as_private(options = {:noop => true, :subject => nil})
RSpec.configure do |config|
config.mock_with :rspec
config.color_enabled = true
- config.order = :random
+ # config.order = :random
@dchelimsky Owner

Why is this commented out? Was it causing you a problem?

Only when I tried to run individual specs. eg rspec spec/mock_ordering_spec.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dchelimsky dchelimsky merged commit b4e6746 into rspec:master
@dchelimsky dchelimsky referenced this pull request from a commit
@dchelimsky dchelimsky Changelog for #101, #106 35f67e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
View
5 spec/rspec/mocks/mock_space_spec.rb
@@ -42,6 +42,11 @@ def reset?
@space.reset_all
@space.instance_eval { mocks.empty? }.should be_true
end
+ it "resets the ordering" do
+ OrderGroup.should_receive(:new)
+ @space.reset_all
+ @space.expectation_ordering
+ end
it "only adds an instance once" do
@space.add(m1 = double("mock1"))
@space.add(m1)
View
2  spec/spec_helper.rb
@@ -36,7 +36,7 @@ def treats_method_missing_as_private(options = {:noop => true, :subject => nil})
RSpec.configure do |config|
config.mock_with :rspec
config.color_enabled = true
- config.order = :random
+ # config.order = :random
@dchelimsky Owner

Why is this commented out? Was it causing you a problem?

Only when I tried to run individual specs. eg rspec spec/mock_ordering_spec.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
config.extend(Macros)
config.include(RSpec::Mocks::Methods)
Something went wrong with that request. Please try again.