Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

replace @expectations & @stubs with expectations_hash

  • Loading branch information...
commit b55735c89578d81b5965683d5409f14eaeec071d 1 parent 71a45b7
David Chelimsky dchelimsky authored
Showing with 35 additions and 19 deletions.
  1. +34 −18 lib/rspec/mocks/proxy.rb
  2. +1 −1  spec/rspec/mocks/stub_spec.rb
52 lib/rspec/mocks/proxy.rb
View
@@ -20,9 +20,7 @@ def initialize(target, name=nil, options={})
@name = name
@error_generator = ErrorGenerator.new target, name
@expectation_ordering = OrderGroup.new @error_generator
- @expectations = []
@messages_received = []
- @stubs = []
@proxied_methods = []
@options = options ? DEFAULT_OPTIONS.dup.merge(options) : DEFAULT_OPTIONS
@already_proxied_respond_to = false
@@ -40,26 +38,29 @@ def as_null_object
def add_message_expectation(expected_from, sym, opts={}, &block)
__add sym
warn_if_nil_class sym
- if existing_stub = @stubs.detect {|s| s.sym == sym }
+ if existing_stub = expectations_hash[sym][:stubs].detect {|s| s.sym == sym }
expectation = existing_stub.build_child(expected_from, block_given?? block : nil, 1, opts)
else
expectation = MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
end
- @expectations << expectation
- @expectations.last
+ expectations_hash[sym][:expectations] << expectation
+ expectation
end
def add_negative_message_expectation(expected_from, sym, &block)
__add sym
warn_if_nil_class sym
- @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
- @expectations.last
+ expectation = NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
+ expectations_hash[sym][:expectations] << expectation
+ expectation
end
def add_stub(expected_from, sym, opts={}, &implementation)
__add sym
- @stubs.unshift MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts, &implementation)
- @stubs.first
+
+ stub = MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts, &implementation)
+ expectations_hash[sym][:stubs].unshift stub
+ stub
end
def verify #:nodoc:
@@ -81,7 +82,7 @@ def received_message?(sym, *args, &block)
end
def has_negative_expectation?(sym)
- @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
+ expectations_hash[sym][:expectations].detect {|expectation| expectation.negative_expectation_for?(sym)}
end
def record_message_received(sym, args, block)
@@ -172,11 +173,15 @@ def munge(sym)
end
def clear_expectations
- @expectations.clear
+ expectations_hash.each do |key, value|
+ value[:expectations].clear
+ end
end
def clear_stubs
- @stubs.clear
+ expectations_hash.each do |key, value|
+ value[:stubs].clear
+ end
end
def clear_proxied_methods
@@ -188,8 +193,10 @@ class << @target; self; end
end
def verify_expectations
- @expectations.each do |expectation|
- expectation.verify_messages_received
+ expectations_hash.each_key do |key|
+ expectations_hash[key][:expectations].each do |expectation|
+ expectation.verify_messages_received
+ end
end
end
@@ -215,16 +222,25 @@ def reset_nil_expectations_warning
end
def find_matching_expectation(sym, *args)
- @expectations.find {|expectation| expectation.matches(sym, args) && !expectation.called_max_times?} ||
- @expectations.find {|expectation| expectation.matches(sym, args)}
+ expectations_hash[sym][:expectations].find {|expectation| expectation.matches(sym, args) && !expectation.called_max_times?} ||
+ expectations_hash[sym][:expectations].find {|expectation| expectation.matches(sym, args)}
end
def find_almost_matching_expectation(sym, *args)
- @expectations.find {|expectation| expectation.matches_name_but_not_args(sym, args)}
+ expectations_hash[sym][:expectations].find {|expectation| expectation.matches_name_but_not_args(sym, args)}
end
def find_matching_method_stub(sym, *args)
- @stubs.find {|stub| stub.matches(sym, args)}
+ expectations_hash[sym][:stubs].find {|stub| stub.matches(sym, args)}
+ end
+
+ def expectations_hash
+ @expectations_hash ||= Hash.new {|h,k|
+ h[k] = {
+ :expectations => [],
+ :stubs => []
+ }
+ }
end
end
2  spec/rspec/mocks/stub_spec.rb
View
@@ -57,7 +57,7 @@ def existing_instance_method
end
it "should clear itself when verified" do
- @instance.stub!(:this_should_go).and_return(:blah)
+ @instance.stub(:this_should_go).and_return(:blah)
@instance.this_should_go.should == :blah
@instance.rspec_verify
lambda do
Please sign in to comment.
Something went wrong with that request. Please try again.