class RSpec::Expectations::ExpectationTarget

Wraps the target of an expectation. @example

expect(something) # => ExpectationTarget wrapping something

Public Class Methods

new(target) click to toggle source

@api private

# File lib/rspec/expectations/expectation_target.rb, line 8
def initialize(target)
  @target = target
end

Public Instance Methods

not_to(matcher=nil, message=nil, &block)
Alias for: to_not
to(matcher=nil, message=nil, &block) click to toggle source

Runs the given expectation, passing if `matcher` returns true. @example

expect(value).to eq(5)
expect { perform }.to raise_error

@param [Matcher]

matcher

@param [String] message optional message to display when the expectation fails @return [Boolean] true if the expectation succeeds (else raises) @see RSpec::Matchers

# File lib/rspec/expectations/expectation_target.rb, line 21
def to(matcher=nil, message=nil, &block)
  prevent_operator_matchers(:to, matcher)
  RSpec::Expectations::PositiveExpectationHandler.handle_matcher(@target, matcher, message, &block)
end
to_not(matcher=nil, message=nil, &block) click to toggle source

Runs the given expectation, passing if `matcher` returns false. @example

expect(value).to_not eq(5)
expect(value).not_to eq(5)

@param [Matcher]

matcher

@param [String] message optional message to display when the expectation fails @return [Boolean] false if the negative expectation succeeds (else raises) @see RSpec::Matchers

# File lib/rspec/expectations/expectation_target.rb, line 35
def to_not(matcher=nil, message=nil, &block)
  prevent_operator_matchers(:to_not, matcher)
  RSpec::Expectations::NegativeExpectationHandler.handle_matcher(@target, matcher, message, &block)
end
Also aliased as: not_to

Private Instance Methods

prevent_operator_matchers(verb, matcher) click to toggle source
# File lib/rspec/expectations/expectation_target.rb, line 43
def prevent_operator_matchers(verb, matcher)
  return if matcher

  raise ArgumentError, "The expect syntax does not support operator matchers, " +
                       "so you must pass a matcher to `##{verb}`."
end