Skip to content

Commit

Permalink
allow unstubing of instances that are a sub class
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Aug 20, 2013
1 parent 9141d6c commit abf019f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/rspec/mocks/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ def remove_any_instance_recorder_for(klass)
end

def proxies_of(klass)
proxies_by_klass[klass].map { |id| proxies[id] }
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
end

def proxy_for(object)
Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/mocks/any_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ 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 @@ -287,6 +289,14 @@ class RSpec::SampleRspecTestClass;end
expect(obj.existing_method).to eq(:existing_method_return_value)
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
klass.any_instance.unstub(:existing_method)
expect(obj.existing_method).to eq(:existing_method_return_value)
end

it "does not remove any stubs on the local instance" do
klass.any_instance.stub(:existing_method).and_return(:any_instance_value)
obj = klass.new
Expand Down
13 changes: 10 additions & 3 deletions spec/rspec/mocks/space_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
module RSpec::Mocks
describe Space do

describe "#proxies_of_any_instance_recorder(klass)" do
describe "#proxies_of(klass)" do
let(:space) { Space.new }

before do
space.proxy_for(Object.new)
space.proxy_for(String.new)
space.proxy_for(Object.new)
space.proxy_for(Array.new)
space.proxy_for(String.new)
end

it 'returns the proxies for objects of the same type as klass' do
expect(space.proxies_of(String).size).to eq 2
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)).to be_all { |obj| obj.is_a? RSpec::Mocks::PartialMockProxy }
end
end

Expand Down

0 comments on commit abf019f

Please sign in to comment.