Skip to content

Commit

Permalink
refactor lookup for subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Aug 21, 2013
1 parent abf019f commit a8b330b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
3 changes: 3 additions & 0 deletions lib/rspec/mocks/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def initialize(object, name=nil, options={})
@null_object = false
end

# @private
attr_reader :object

# @private
def null_object?
@null_object
Expand Down
18 changes: 2 additions & 16 deletions lib/rspec/mocks/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ module RSpec
module Mocks
# @api private
class Space
attr_reader :proxies, :any_instance_recorders, :proxies_by_klass
attr_reader :proxies, :any_instance_recorders

def initialize
@proxies = {}
@any_instance_recorders = {}
@proxies_by_klass = Hash.new { |hash, key| hash[key] = [] }
end

def verify_all
Expand All @@ -29,7 +28,6 @@ def reset_all

proxies.clear
any_instance_recorders.clear
proxies_by_klass.clear
expectation_ordering.clear
end

Expand All @@ -49,15 +47,7 @@ def remove_any_instance_recorder_for(klass)
end

def proxies_of(klass)
proxies_by_klass.inject([]) do |klass_proxies, (proxy_klass, object_ids)|
more_proxies =
if proxy_klass.ancestors.include?(klass) || klass.ancestors.include?(proxy_klass)
object_ids.map { |id| proxies[id] }
else
[]
end
klass_proxies + more_proxies
end
proxies.values.select { |proxy| klass === proxy.object }
end

def proxy_for(object)
Expand All @@ -66,11 +56,7 @@ def proxy_for(object)
proxies[id] = case object
when NilClass then ProxyForNil.new
when TestDouble then object.__build_mock_proxy
when Class then
proxies_by_klass[object] << id
PartialMockProxy.new(object)
else
proxies_by_klass[object.class] << id
PartialMockProxy.new(object)
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/rspec/mocks/any_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ class RSpec::SampleRspecTestClass;end
end

context "unstub implementation" do
let(:sub_klass) { Class.new(klass) }

it "replaces the stubbed method with the original method" do
klass.any_instance.stub(:existing_method)
klass.any_instance.unstub(:existing_method)
Expand All @@ -291,8 +289,8 @@ class RSpec::SampleRspecTestClass;end

it "removes stubs from sub class after invokation when super class was originally stubbed" do
klass.any_instance.stub(:existing_method).and_return(:any_instance_value)
obj = sub_klass.new
obj.existing_method
obj = Class.new(klass).new
expect(obj.existing_method).to eq(:any_instance_value)
klass.any_instance.unstub(:existing_method)
expect(obj.existing_method).to eq(:existing_method_return_value)
end
Expand Down
7 changes: 1 addition & 6 deletions spec/rspec/mocks/space_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ module RSpec::Mocks
end

it 'returns the proxies for objects of the same type as klass' do
expect(space.proxies_of(Object).size).to eq 4
expect(space.proxies_of(Object)).to be_all { |obj| obj.is_a? RSpec::Mocks::PartialMockProxy }
end

it 'returns the proxies of super classes of klass' do
expect(space.proxies_of(String).size).to eq 3
expect(space.proxies_of(String).size).to eq 1
expect(space.proxies_of(String)).to be_all { |obj| obj.is_a? RSpec::Mocks::PartialMockProxy }
end
end
Expand Down

0 comments on commit a8b330b

Please sign in to comment.