spec/rspec/mocks/any_instance_spec.rb in rspec-mocks-2.99.0.beta2 vs spec/rspec/mocks/any_instance_spec.rb in rspec-mocks-2.99.0.rc1

- old
+ new

@@ -46,16 +46,16 @@ it "raises an error if 'should_receive' follows 'with'" do expect { klass.any_instance.with("1").should_receive(:foo) }.to raise_error(NoMethodError) end it "raises an error if 'with' follows 'and_return'" do - pending "see Github issue #42" + skip "see Github issue #42" expect { klass.any_instance.should_receive(:foo).and_return(1).with("1") }.to raise_error(NoMethodError) end it "raises an error if 'with' follows 'and_raise'" do - pending "see Github issue #42" + skip "see Github issue #42" expect { klass.any_instance.should_receive(:foo).and_raise(1).with("1") }.to raise_error(NoMethodError) end end end @@ -375,10 +375,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 { @@ -397,28 +398,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) @@ -476,28 +489,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 @@ -586,20 +611,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 @@ -720,13 +753,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 @@ -758,13 +795,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 @@ -785,39 +826,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 @@ -836,10 +881,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 @@ -883,10 +929,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 @@ -895,10 +942,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 @@ -1176,9 +1224,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