diff --git a/lib/rspec/mocks.rb b/lib/rspec/mocks.rb index 17b2831bc..738a72df3 100644 --- a/lib/rspec/mocks.rb +++ b/lib/rspec/mocks.rb @@ -117,6 +117,7 @@ class << self # Namespace for mock-related matchers. module Matchers + autoload :Matcher, "rspec/mocks/matchers/matcher" autoload :HaveReceived, "rspec/mocks/matchers/have_received" autoload :Receive, "rspec/mocks/matchers/receive" autoload :ReceiveMessageChain, "rspec/mocks/matchers/receive_message_chain" diff --git a/lib/rspec/mocks/matchers/have_received.rb b/lib/rspec/mocks/matchers/have_received.rb index ec3248c40..caa7e20b9 100644 --- a/lib/rspec/mocks/matchers/have_received.rb +++ b/lib/rspec/mocks/matchers/have_received.rb @@ -3,6 +3,8 @@ module Mocks module Matchers # @private class HaveReceived + include Matcher + COUNT_CONSTRAINTS = %w[exactly at_least at_most times once twice thrice] ARGS_CONSTRAINTS = %w[with] CONSTRAINTS = COUNT_CONSTRAINTS + ARGS_CONSTRAINTS + %w[ordered] diff --git a/lib/rspec/mocks/matchers/matcher.rb b/lib/rspec/mocks/matchers/matcher.rb new file mode 100644 index 000000000..19414b870 --- /dev/null +++ b/lib/rspec/mocks/matchers/matcher.rb @@ -0,0 +1,8 @@ +module RSpec + module Mocks + module Matchers + # just a "tag" for rspec-mock matchers detection + module Matcher; end + end + end +end diff --git a/lib/rspec/mocks/matchers/receive.rb b/lib/rspec/mocks/matchers/receive.rb index a16b28fb3..980a01dde 100644 --- a/lib/rspec/mocks/matchers/receive.rb +++ b/lib/rspec/mocks/matchers/receive.rb @@ -5,6 +5,8 @@ module Mocks module Matchers # @private class Receive + include Matcher + def initialize(message, block) @message = message @block = block diff --git a/lib/rspec/mocks/matchers/receive_message_chain.rb b/lib/rspec/mocks/matchers/receive_message_chain.rb index d831cd3ac..583ecf70a 100644 --- a/lib/rspec/mocks/matchers/receive_message_chain.rb +++ b/lib/rspec/mocks/matchers/receive_message_chain.rb @@ -5,6 +5,8 @@ module Mocks module Matchers # @private class ReceiveMessageChain + include Matcher + def initialize(chain, &block) @chain = chain @block = block diff --git a/lib/rspec/mocks/matchers/receive_messages.rb b/lib/rspec/mocks/matchers/receive_messages.rb index b6316aa45..218d238e3 100644 --- a/lib/rspec/mocks/matchers/receive_messages.rb +++ b/lib/rspec/mocks/matchers/receive_messages.rb @@ -3,6 +3,8 @@ module Mocks module Matchers # @private class ReceiveMessages + include Matcher + def initialize(message_return_value_hash) @message_return_value_hash = message_return_value_hash @backtrace_line = CallerFilter.first_non_rspec_line diff --git a/lib/rspec/mocks/targets.rb b/lib/rspec/mocks/targets.rb index 5777810b9..61375b101 100644 --- a/lib/rspec/mocks/targets.rb +++ b/lib/rspec/mocks/targets.rb @@ -38,7 +38,7 @@ def self.disallow_negation(method_name) private def matcher_allowed?(matcher) - matcher.class.name.start_with?("RSpec::Mocks::Matchers".freeze) + Matchers::Matcher === matcher end def define_matcher(matcher, name, &block) diff --git a/spec/rspec/mocks/matchers/receive_spec.rb b/spec/rspec/mocks/matchers/receive_spec.rb index 92f34e450..7a72ced59 100644 --- a/spec/rspec/mocks/matchers/receive_spec.rb +++ b/spec/rspec/mocks/matchers/receive_spec.rb @@ -90,6 +90,12 @@ module Mocks }.to raise_error(UnsupportedMatcherError) end + it 'does support inherited matchers', :unless => options.include?(:allow_other_matchers) do + receive_foo = Class.new(RSpec::Mocks::Matchers::Receive).new(:foo, nil) + wrapped.to receive_foo + receiver.foo + end + it 'does not get confused by messages being passed as strings and symbols' do wrapped.to receive(:foo).with(1) { :a } wrapped.to receive("foo").with(2) { :b }