spec/rspec/mocks/any_instance_spec.rb in rspec-mocks-2.12.2 vs spec/rspec/mocks/any_instance_spec.rb in rspec-mocks-2.13.0
- old
+ new
@@ -1,6 +1,7 @@
require 'spec_helper'
+require 'delegate'
module RSpec
module Mocks
describe "#any_instance" do
class CustomErrorForAnyInstanceSpec < StandardError;end
@@ -243,10 +244,18 @@
klass.any_instance.stub!(:foo)
end.to raise_error(/Use stub instead/)
end
end
+ context "with #unstub!" do
+ it "raises with a message instructing the user to use unstub instead" do
+ expect do
+ klass.any_instance.unstub!(:foo)
+ end.to raise_error(/Use unstub instead/)
+ end
+ end
+
context "unstub implementation" do
it "replaces the stubbed method with the original method" do
klass.any_instance.stub(:existing_method)
klass.any_instance.unstub(:existing_method)
expect(klass.new.existing_method).to eq(:existing_method_return_value)
@@ -300,10 +309,19 @@
it "passes if the method is called with different parameters" do
klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)
expect { klass.new.existing_method_with_arguments(:argument_three, :argument_four) }.to_not raise_error
end
end
+
+ context 'when used in combination with should_receive' do
+ 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
+ klass.rspec_verify
+ end
+ end
end
context "with #should_receive" do
let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }
let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }
@@ -432,9 +450,28 @@
instance_one.existing_method
instance_two.existing_method
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
end
end
+ end
+
+ it 'works with a BasicObject subclass that mixes in Kernel', :if => defined?(BasicObject) do
+ klass = Class.new(BasicObject) do
+ include ::Kernel
+ def foo; end
+ end
+
+ klass.any_instance.should_receive(:foo)
+ klass.new.foo
+ end
+
+ it 'works with a SimpleDelegator subclass', :if => (RUBY_VERSION.to_f > 1.8) do
+ klass = Class.new(SimpleDelegator) do
+ def foo; end
+ end
+
+ klass.any_instance.should_receive(:foo)
+ klass.new(Object.new).foo
end
context "with argument matching" do
before do
klass.any_instance.should_receive(:foo).with(:param_one, :param_two).and_return(:result_one)