class RSpec::Expectations::BlockExpectationTarget

@private Validates the provided matcher to ensure it supports block expectations, in order to avoid user confusion when they use a block thinking the expectation will be on the return value of the block rather than the block itself.

Public Instance Methods

not_to(matcher, message=nil, &block) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 142
def not_to(matcher, message=nil, &block)
  enforce_block_expectation(matcher)
  super
end
Also aliased as: to_not
to(matcher, message=nil, &block) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 137
def to(matcher, message=nil, &block)
  enforce_block_expectation(matcher)
  super
end
to_not(matcher, message=nil, &block)
Alias for: not_to

Private Instance Methods

enforce_block_expectation(matcher) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 150
def enforce_block_expectation(matcher)
  return if supports_block_expectations?(matcher)

  raise ExpectationNotMetError, "You must pass an argument rather than a block to `expect` to use the provided " \
    "matcher (#{RSpec::Support::ObjectFormatter.format(matcher)}), or the matcher must implement " \
    "`supports_block_expectations?`."
end
supports_block_expectations?(matcher) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 158
def supports_block_expectations?(matcher)
  matcher.respond_to?(:supports_block_expectations?) && matcher.supports_block_expectations?
end