class RSpec::Expectations::ValueExpectationTarget

@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=nil, message=nil, &block) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 104
def not_to(matcher=nil, message=nil, &block)
  enforce_value_expectation(matcher)
  super
end
to(matcher=nil, message=nil, &block) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 99
def to(matcher=nil, message=nil, &block)
  enforce_value_expectation(matcher)
  super
end

Private Instance Methods

enforce_value_expectation(matcher) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 111
def enforce_value_expectation(matcher)
  return if supports_value_expectations?(matcher)

  RSpec.deprecate(
    "expect(value).to #{RSpec::Support::ObjectFormatter.format(matcher)}",
    :message =>
      "The implicit block expectation syntax is deprecated, you should pass " \
      "a block rather than an argument to `expect` to use the provided " \
      "block expectation matcher or the matcher must implement " \
      "`supports_value_expectations?`. e.g  `expect { value }.to " \
      "#{RSpec::Support::ObjectFormatter.format(matcher)}` not " \
      "`expect(value).to #{RSpec::Support::ObjectFormatter.format(matcher)}`"
  )
end
supports_value_expectations?(matcher) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 126
def supports_value_expectations?(matcher)
  !matcher.respond_to?(:supports_value_expectations?) || matcher.supports_value_expectations?
end