Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #416 from rspec/any_instance_resetting

`any_instance` partial mocks aren't reset
  • Loading branch information...
commit 331b4b3c48a48fc98cad3c354a78f1ff14d2b99d 2 parents fd78578 + 08c6637
@JonRowe JonRowe authored
View
18 spec/rspec/mocks/any_instance_spec.rb
@@ -225,6 +225,24 @@ def private_method; :private_method_return_value; end
end
end
+ context "when partially mocking objects" do
+ let(:obj) { klass.new }
+
+ it "resets partially mocked objects correctly" do
+ allow_any_instance_of(klass).to receive(:existing_method).and_return("stubbed value")
+
+ # Simply resetting the proxy doesn't work
+ # what we need to have happen is
+ # ::RSpec::Mocks.any_instance_recorder_for(klass).stop_all_observation!
+ # but that is never invoked in ::
+ expect {
+ RSpec::Mocks.space.verify_all
+ }.to(
+ change { obj.existing_method }.from("stubbed value").to(:existing_method_return_value)
+ )
+ end
+ end
+
context "core ruby objects" do
it "works uniformly across *everything*" do
Object.any_instance.stub(:foo).and_return(1)
View
34 spec/rspec/mocks/matchers/receive_spec.rb
@@ -148,11 +148,36 @@ def receiver.method_missing(*a); end # a poor man's stub...
end
end
+ shared_examples_for "resets partial mocks cleanly" do
+ let(:klass) { Struct.new(:foo) }
+ let(:object) { klass.new :bar }
+
+ it "removes the method double" do
+ target.to receive(:foo).and_return(:baz)
+ expect { reset object }.to change { object.foo }.from(:baz).to(:bar)
+ end
+ end
+
+ shared_examples_for "resets partial mocks of any instance cleanly" do
+ let(:klass) { Struct.new(:foo) }
+ let(:object) { klass.new :bar }
+
+ it "removes the method double" do
+ target.to receive(:foo).and_return(:baz)
+ expect {
+ ::RSpec::Mocks.space.verify_all
+ }.to change { object.foo }.from(:baz).to(:bar)
+ end
+ end
+
describe "allow(...).to receive" do
include_examples "an expect syntax allowance" do
let(:receiver) { double }
let(:wrapped) { allow(receiver) }
end
+ include_examples "resets partial mocks cleanly" do
+ let(:target) { allow(object) }
+ end
end
describe "allow(...).not_to receive" do
@@ -167,6 +192,9 @@ def receiver.method_missing(*a); end # a poor man's stub...
let(:wrapped) { allow_any_instance_of(klass) }
let(:receiver) { klass.new }
end
+ include_examples "resets partial mocks of any instance cleanly" do
+ let(:target) { allow_any_instance_of(klass) }
+ end
end
describe "allow_any_instance_of(...).not_to receive" do
@@ -180,6 +208,9 @@ def receiver.method_missing(*a); end # a poor man's stub...
let(:receiver) { double }
let(:wrapped) { expect(receiver) }
end
+ include_examples "resets partial mocks cleanly" do
+ let(:target) { expect(object) }
+ end
end
describe "expect_any_instance_of(...).to receive" do
@@ -188,6 +219,9 @@ def receiver.method_missing(*a); end # a poor man's stub...
let(:wrapped) { expect_any_instance_of(klass) }
let(:receiver) { klass.new }
end
+ include_examples "resets partial mocks of any instance cleanly" do
+ let(:target) { expect_any_instance_of(klass) }
+ end
end
describe "expect(...).not_to receive" do
Please sign in to comment.
Something went wrong with that request. Please try again.