Class: RSpec::Expectations::ExpectationTarget

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

Overview

Wraps the target of an expectation.

Examples:

expect(something) # => ExpectationTarget wrapping something

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

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ 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.



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

def initialize(target)
  @target = target
end

Class Attribute Details

.deprecated_should_enabledObject Also known as: deprecated_should_enabled?

Returns the value of attribute deprecated_should_enabled.



14
15
16
# File 'lib/rspec/expectations/expectation_target.rb', line 14

def deprecated_should_enabled
  @deprecated_should_enabled
end

Class Method Details

.disable_deprecated_shouldObject



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

def self.disable_deprecated_should
  return unless deprecated_should_enabled?

  remove_method :should
  remove_method :should_not

  self.deprecated_should_enabled = false
end

.enable_deprecated_shouldObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspec/expectations/expectation_target.rb', line 52

def self.enable_deprecated_should
  return if deprecated_should_enabled?

  def should(*args)
    RSpec.deprecate "`expect { }.should`", "`expect { }.to`", 3
    @target.should(*args)
  end

  def should_not(*args)
    RSpec.deprecate "`expect { }.should_not`", "`expect { }.to_not`", 3
    @target.should_not(*args)
  end

  self.deprecated_should_enabled = true
end

Instance Method Details

#should(*args) ⇒ Object



55
56
57
58
# File 'lib/rspec/expectations/expectation_target.rb', line 55

def should(*args)
  RSpec.deprecate "`expect { }.should`", "`expect { }.to`", 3
  @target.should(*args)
end

#should_not(*args) ⇒ Object



60
61
62
63
# File 'lib/rspec/expectations/expectation_target.rb', line 60

def should_not(*args)
  RSpec.deprecate "`expect { }.should_not`", "`expect { }.to_not`", 3
  @target.should_not(*args)
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) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:



32
33
34
35
# File 'lib/rspec/expectations/expectation_target.rb', line 32

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) ⇒ Boolean Also known as: not_to

Runs the given expectation, passing if matcher returns false.

Examples:

expect(value).to_not eq(5)
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:



46
47
48
49
# File 'lib/rspec/expectations/expectation_target.rb', line 46

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