Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

issue #124: adding support for any_instance.should_not_receive

  • Loading branch information...
commit 6e858e4088c2813ebb29ae7d17c26d8aa1ea635a 1 parent 419b64a
aakash dharmadhikari aakashd authored
4 lib/rspec/mocks/any_instance.rb
View
@@ -1,4 +1,8 @@
require 'rspec/mocks/any_instance/chain'
+require 'rspec/mocks/any_instance/stub_chain'
+require 'rspec/mocks/any_instance/stub_chain_chain'
+require 'rspec/mocks/any_instance/expectation_chain'
+require 'rspec/mocks/any_instance/negative_expectation_chain'
require 'rspec/mocks/any_instance/message_chains'
require 'rspec/mocks/any_instance/recorder'
81 lib/rspec/mocks/any_instance/chain.rb
View
@@ -72,87 +72,6 @@ def record(rspec_method_name, *args, &block)
self
end
end
-
- # @private
- class ExpectationChain < Chain
-
- # @private
- def initialize(*args, &block)
- record(:should_receive, *args, &block)
- @expectation_fulfilled = false
- end
-
- # @private
- def expectation_fulfilled?
- @expectation_fulfilled || constrained_to_any_of?(:never, :any_number_of_times)
- end
-
- private
-
- def verify_invocation_order(rspec_method_name, *args, &block)
- end
-
- def invocation_order
- @invocation_order ||= {
- :should_receive => [nil],
- :with => [:should_receive],
- :and_return => [:with, :should_receive],
- :and_raise => [:with, :should_receive]
- }
- end
- end
-
- # @private
- class StubChain < Chain
-
- # @private
- def initialize(*args, &block)
- record(:stub, *args, &block)
- end
-
- # @private
- def expectation_fulfilled?
- true
- end
-
- private
-
- def invocation_order
- @invocation_order ||= {
- :stub => [nil],
- :with => [:stub],
- :and_return => [:with, :stub],
- :and_raise => [:with, :stub],
- :and_yield => [:with, :stub]
- }
- end
-
- def verify_invocation_order(rspec_method_name, *args, &block)
- unless invocation_order[rspec_method_name].include?(last_message)
- raise(NoMethodError, "Undefined method #{rspec_method_name}")
- end
- end
- end
-
- # @private
- class StubChainChain < StubChain
-
- # @private
- def initialize(*args, &block)
- record(:stub_chain, *args, &block)
- end
-
- private
-
- def invocation_order
- @invocation_order ||= {
- :stub_chain => [nil],
- :and_return => [:stub_chain],
- :and_raise => [:stub_chain],
- :and_yield => [:stub_chain]
- }
- end
- end
end
end
end
35 lib/rspec/mocks/any_instance/expectation_chain.rb
View
@@ -0,0 +1,35 @@
+module RSpec
+ module Mocks
+ module AnyInstance
+
+ # @private
+ class ExpectationChain < Chain
+
+ # @private
+ def initialize(*args, &block)
+ record(:should_receive, *args, &block)
+ @expectation_fulfilled = false
+ end
+
+ # @private
+ def expectation_fulfilled?
+ @expectation_fulfilled || constrained_to_any_of?(:never, :any_number_of_times)
+ end
+
+ private
+
+ def verify_invocation_order(rspec_method_name, *args, &block)
+ end
+
+ def invocation_order
+ @invocation_order ||= {
+ :should_receive => [nil],
+ :with => [:should_receive],
+ :and_return => [:with, :should_receive],
+ :and_raise => [:with, :should_receive]
+ }
+ end
+ end
+ end
+ end
+end
33 lib/rspec/mocks/any_instance/negative_expectation_chain.rb
View
@@ -0,0 +1,33 @@
+module RSpec
+ module Mocks
+ module AnyInstance
+ # @private
+ class NegativeExpectationChain < Chain
+ # @private
+ def initialize(*args, &block)
+ record(:should_not_receive, *args, &block)
+ @expectation_fulfilled = false
+ end
+
+ # @private
+ def expectation_fulfilled?
+ @expectation_fulfilled || constrained_to_any_of?(:never, :any_number_of_times)
+ end
+
+ private
+
+ def verify_invocation_order(rspec_method_name, *args, &block)
+ end
+
+ def invocation_order
+ @invocation_order ||= {
+ :should_not_receive => [nil],
+ :with => [:should_receive],
+ :and_return => [:with, :should_receive],
+ :and_raise => [:with, :should_receive]
+ }
+ end
+ end
+ end
+ end
+end
5 lib/rspec/mocks/any_instance/recorder.rb
View
@@ -59,6 +59,11 @@ def should_receive(method_name, &block)
message_chains.add(method_name, ExpectationChain.new(method_name, &block))
end
+ def should_not_receive(method_name, &block)
+ observe!(method_name)
+ message_chains.add(method_name, NegativeExpectationChain.new(method_name, &block))
+ end
+
# Removes any previously recorded stubs, stub_chains or message
# expectations that use `method_name`.
#
37 lib/rspec/mocks/any_instance/stub_chain.rb
View
@@ -0,0 +1,37 @@
+module RSpec
+ module Mocks
+ module AnyInstance
+ # @private
+ class StubChain < Chain
+
+ # @private
+ def initialize(*args, &block)
+ record(:stub, *args, &block)
+ end
+
+ # @private
+ def expectation_fulfilled?
+ true
+ end
+
+ private
+
+ def invocation_order
+ @invocation_order ||= {
+ :stub => [nil],
+ :with => [:stub],
+ :and_return => [:with, :stub],
+ :and_raise => [:with, :stub],
+ :and_yield => [:with, :stub]
+ }
+ end
+
+ def verify_invocation_order(rspec_method_name, *args, &block)
+ unless invocation_order[rspec_method_name].include?(last_message)
+ raise(NoMethodError, "Undefined method #{rspec_method_name}")
+ end
+ end
+ end
+ end
+ end
+end
25 lib/rspec/mocks/any_instance/stub_chain_chain.rb
View
@@ -0,0 +1,25 @@
+module RSpec
+ module Mocks
+ module AnyInstance
+ # @private
+ class StubChainChain < StubChain
+
+ # @private
+ def initialize(*args, &block)
+ record(:stub_chain, *args, &block)
+ end
+
+ private
+
+ def invocation_order
+ @invocation_order ||= {
+ :stub_chain => [nil],
+ :and_return => [:stub_chain],
+ :and_raise => [:stub_chain],
+ :and_yield => [:stub_chain]
+ }
+ end
+ end
+ end
+ end
+end
25 spec/rspec/mocks/any_instance_spec.rb
View
@@ -264,7 +264,7 @@ class RSpec::SampleRspecTestClass;end
klass.any_instance.stub(:existing_method_with_arguments).with(1)
klass.any_instance.stub(:existing_method_with_arguments).with(2)
klass.any_instance.unstub(:existing_method_with_arguments)
- klass.new.existing_method_with_arguments(3).should eq :three
+ klass.new.existing_method_with_arguments(3).should eq(:three)
end
it "raises a MockExpectationError if the method has not been stubbed" do
@@ -279,6 +279,29 @@ class RSpec::SampleRspecTestClass;end
klass.any_instance.should_not_receive(:existing_method)
lambda { klass.new.existing_method }.should raise_error(RSpec::Mocks::MockExpectationError)
end
+
+ it "passes if no method is called" do
+ lambda { klass.any_instance.should_not_receive(:existing_method) }.should_not raise_error
+ end
+
+ it "passes if only a different method is called" do
+ klass.any_instance.should_not_receive(:existing_method)
+ lambda { klass.new.another_existing_method }.should_not raise_error
+ end
+
+ context "with constraints" do
+ it "fails if the method is called with the specified parameters" do
+ klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)
+ lambda do
+ klass.new.existing_method_with_arguments(:argument_one, :argument_two)
+ end.should raise_error(RSpec::Mocks::MockExpectationError)
+ end
+
+ 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)
+ lambda { klass.new.existing_method_with_arguments(:argument_three, :argument_four) }.should_not raise_error
+ end
+ end
end
context "with #should_receive" do
Please sign in to comment.
Something went wrong with that request. Please try again.