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

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ExpectationTarget) initialize(value)

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.

A new instance of ExpectationTarget



30
31
32
# File 'lib/rspec/expectations/expectation_target.rb', line 30

def initialize(value)
  @target = value
end

Class Attribute Details

+ (Object) deprecated_should_enabled Also known as: deprecated_should_enabled?

Returns the value of attribute deprecatedshouldenabled



19
20
21
# File 'lib/rspec/expectations/expectation_target.rb', line 19

def deprecated_should_enabled
  @deprecated_should_enabled
end

Class Method Details

+ (Object) disable_deprecated_should



92
93
94
95
96
97
98
99
# File 'lib/rspec/expectations/expectation_target.rb', line 92

def self.disable_deprecated_should
  return unless deprecated_should_enabled?

  remove_method :should
  remove_method :should_not

  self.deprecated_should_enabled = false
end

+ (Object) enable_deprecated_should



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rspec/expectations/expectation_target.rb', line 76

def self.enable_deprecated_should
  return if deprecated_should_enabled?

  def should(*args)
    RSpec.deprecate "`expect { }.should`", :replacement => "`expect { }.to`"
    @target.should(*args)
  end

  def should_not(*args)
    RSpec.deprecate "`expect { }.should_not`", :replacement => "`expect { }.not_to`"
    @target.should_not(*args)
  end

  self.deprecated_should_enabled = true
end

Instance Method Details

- (Boolean) not_to(matcher = nil, message = nil) 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) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    false if the negative expectation succeeds (else raises)

See Also:



70
71
72
73
# File 'lib/rspec/expectations/expectation_target.rb', line 70

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

- (Object) should(*args)



79
80
81
82
# File 'lib/rspec/expectations/expectation_target.rb', line 79

def should(*args)
  RSpec.deprecate "`expect { }.should`", :replacement => "`expect { }.to`"
  @target.should(*args)
end

- (Object) should_not(*args)



84
85
86
87
# File 'lib/rspec/expectations/expectation_target.rb', line 84

def should_not(*args)
  RSpec.deprecate "`expect { }.should_not`", :replacement => "`expect { }.not_to`"
  @target.should_not(*args)
end

- (Boolean) to(matcher = nil, message = nil)

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) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:



57
58
59
60
# File 'lib/rspec/expectations/expectation_target.rb', line 57

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