Class: RSpec::Expectations::ExpectationTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/expectations/expectation_target.rb

Overview

Note:

ExpectationTarget is not intended to be instantiated directly by users. Use expect instead.

Wraps the target of an expectation.

Examples:

expect(something)       # => ExpectationTarget wrapping something
expect { do_something } # => ExpectationTarget wrapping the block

# used with `to`
expect(actual).to eq(3)

# with `not_to`
expect(actual).not_to eq(3)

Constant Summary

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ ExpectationTarget

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ExpectationTarget



25
26
27
# File 'lib/rspec/expectations/expectation_target.rb', line 25

def initialize(value)
  @target = value
end

Instance Method Details

#not_to(matcher = nil, message = nil, &block) ⇒ Boolean Also known as: to_not

Runs the given expectation, passing if matcher returns false.

Examples:

expect(value).not_to eq(5)

Parameters:

  • matcher (Matcher) (defaults to: nil)
  • message (String or Proc) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    false if the negative expectation succeeds (else raises)

See Also:



65
66
67
68
# File 'lib/rspec/expectations/expectation_target.rb', line 65

def not_to(matcher=nil, message=nil, &block)
  prevent_operator_matchers(:not_to) unless matcher
  RSpec::Expectations::NegativeExpectationHandler.handle_matcher(@target, matcher, message, &block)
end

#to(matcher = nil, message = nil, &block) ⇒ Boolean

Runs the given expectation, passing if matcher returns true.

Examples:

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

Parameters:

  • matcher (Matcher) (defaults to: nil)
  • message (String or Proc) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:



52
53
54
55
# File 'lib/rspec/expectations/expectation_target.rb', line 52

def to(matcher=nil, message=nil, &block)
  prevent_operator_matchers(:to) unless matcher
  RSpec::Expectations::PositiveExpectationHandler.handle_matcher(@target, matcher, message, &block)
end