123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Expectations::ExpectationTarget::InstanceMethods

Overview

Defines instance ::RSpec::Expectations::ExpectationTarget instance methods. These are defined in a module so we can include it in Minitest::Expectation when rspec/expectations/minitest_integration is loaded in order to support usage with Minitest.

Instance Method Summary

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

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    false if the negative expectation succeeds (else raises)

See Also:

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/expectation_target.rb', line 76

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

#prevent_operator_matchers(verb) (private)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/expectation_target.rb', line 84

def prevent_operator_matchers(verb)
  raise ArgumentError, "The expect syntax does not support operator matchers, " \
                       "so you must pass a matcher to `##{verb}`."
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, Proc) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/expectation_target.rb', line 63

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

#to_not(matcher = nil, message = nil, &block)

Alias for #not_to.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/expectation_target.rb', line 80

alias to_not not_to