spec/rspec/mocks/any_instance_spec.rb in rspec-mocks-2.14.5 vs spec/rspec/mocks/any_instance_spec.rb in rspec-mocks-2.14.6
- old
+ new
@@ -367,10 +367,11 @@
it 'passes if only the expected message is received' do
klass.any_instance.should_receive(:foo)
klass.any_instance.should_not_receive(:bar)
klass.new.foo
RSpec::Mocks.space.verify_all
+ RSpec::Mocks.space.reset_all
end
end
it "prevents confusing double-negative expressions involving `never`" do
expect {
@@ -389,28 +390,40 @@
expect(klass.new.foo(1)).to eq(1)
end
it "fails if an instance is created but no invocation occurs" do
expect do
- klass.any_instance.should_receive(:foo)
- klass.new
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo)
+ klass.new
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails if no instance is created" do
expect do
- klass.any_instance.should_receive(:foo).and_return(1)
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo).and_return(1)
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails if no instance is created and there are multiple expectations" do
expect do
- klass.any_instance.should_receive(:foo)
- klass.any_instance.should_receive(:bar)
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo)
+ klass.any_instance.should_receive(:bar)
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')
end
it "allows expectations on instances to take priority" do
klass.any_instance.should_receive(:foo)
@@ -468,28 +481,40 @@
expect(klass.new.existing_method(1)).to eq(1)
end
it "fails if an instance is created but no invocation occurs" do
expect do
- klass.any_instance.should_receive(:existing_method)
- klass.new
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:existing_method)
+ klass.new
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
it "fails if no instance is created" do
expect do
- klass.any_instance.should_receive(:existing_method)
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:existing_method)
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
it "fails if no instance is created and there are multiple expectations" do
expect do
- klass.any_instance.should_receive(:existing_method)
- klass.any_instance.should_receive(:another_existing_method)
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:existing_method)
+ klass.any_instance.should_receive(:another_existing_method)
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: another_existing_method, existing_method')
end
context "after any one instance has received a message" do
it "passes if subsequent invocations do not receive that message" do
@@ -578,20 +603,28 @@
klass.new.foo
end
it "fails when no instances are declared" do
expect do
- klass.any_instance.should_receive(:foo).once
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo).once
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails when an instance is declared but there are no invocations" do
expect do
- klass.any_instance.should_receive(:foo).once
- klass.new
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo).once
+ klass.new
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails for more than one invocation" do
expect do
@@ -712,13 +745,17 @@
expect(klass.new.existing_method).to eq(5)
end
it "fails when the other expecations are not met" do
expect do
- klass.any_instance.should_receive(:foo).never
- klass.any_instance.should_receive(:existing_method).and_return(5)
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo).never
+ klass.any_instance.should_receive(:existing_method).and_return(5)
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
end
end
@@ -750,13 +787,17 @@
expect(klass.new.existing_method).to eq(5)
end
it "fails when the other expecations are not met" do
expect do
- klass.any_instance.should_receive(:foo).any_number_of_times
- klass.any_instance.should_receive(:existing_method).and_return(5)
- RSpec::Mocks.space.verify_all
+ begin
+ klass.any_instance.should_receive(:foo).any_number_of_times
+ klass.any_instance.should_receive(:existing_method).and_return(5)
+ RSpec::Mocks.space.verify_all
+ ensure
+ RSpec::Mocks.space.reset_all
+ end
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
end
end
end
@@ -777,39 +818,43 @@
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_true
end
it "restores the class to its original state after each example when no instance is created" do
space.verify_all
+ space.reset_all
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
expect(klass.new.existing_method).to eq(existing_method_return_value)
end
it "restores the class to its original state after each example when one instance is created" do
klass.new.existing_method
space.verify_all
+ space.reset_all
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
expect(klass.new.existing_method).to eq(existing_method_return_value)
end
it "restores the class to its original state after each example when more than one instance is created" do
klass.new.existing_method
klass.new.existing_method
space.verify_all
+ space.reset_all
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
expect(klass.new.existing_method).to eq(existing_method_return_value)
end
end
context "private methods" do
before :each do
klass.any_instance.stub(:private_method).and_return(:something)
space.verify_all
+ space.reset_all
end
it "cleans up the backed up method" do
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
end
@@ -828,10 +873,11 @@
context "private methods" do
before :each do
klass.any_instance.should_receive(:private_method).and_return(:something)
klass.new.private_method
space.verify_all
+ space.reset_all
end
it "cleans up the backed up method" do
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
end
@@ -875,10 +921,11 @@
it "ensures that the next spec does not see that expectation" do
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
klass.new.existing_method
space.verify_all
+ space.reset_all
expect(klass.new.existing_method).to eq(existing_method_return_value)
end
end
end
@@ -887,10 +934,11 @@
it "does not prevent the change from being rolled back" do
klass.any_instance.stub(:existing_method).and_return(false)
klass.any_instance.stub(:existing_method).and_return(true)
RSpec::Mocks.space.verify_all
+ RSpec::Mocks.space.reset_all
expect(klass.new).to respond_to(:existing_method)
expect(klass.new.existing_method).to eq(existing_method_return_value)
end
end
@@ -976,9 +1024,10 @@
instance = klass.new
expect(instance.existing_method).to eq :stubbed_return_value
RSpec::Mocks.verify
+ RSpec::Mocks.teardown
expect(instance.existing_method).to eq :existing_method_return_value
end
end
end