Module: RSpec::Expectations

### Home Classes Methods

### In Files

- lib/rspec/expectations.rb
- lib/rspec/expectations/deprecation.rb
- lib/rspec/expectations/differ.rb
- lib/rspec/expectations/errors.rb
- lib/rspec/expectations/expectation_target.rb
- lib/rspec/expectations/extensions/object.rb
- lib/rspec/expectations/fail_with.rb
- lib/rspec/expectations/handler.rb
- lib/rspec/expectations/syntax.rb
- lib/rspec/expectations/version.rb

### Namespace

- MODULE RSpec::Expectations::DeprecatedConstants
- MODULE RSpec::Expectations::Deprecation
- MODULE RSpec::Expectations::Syntax
- MODULE RSpec::Expectations::Version
- CLASS RSpec::Expectations::Differ
- CLASS RSpec::Expectations::ExpectationHandler
- CLASS RSpec::Expectations::ExpectationNotMetError
- CLASS RSpec::Expectations::ExpectationTarget
- CLASS RSpec::Expectations::NegativeExpectationHandler
- CLASS RSpec::Expectations::PositiveExpectationHandler

### Methods

- ::differ
- ::fail_with
- #differ=

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Expectations
- RSpec::Expectations::DeprecatedConstants
- RSpec::Expectations::Deprecation
- RSpec::Expectations::Differ
- RSpec::Expectations::ExpectationHandler
- RSpec::Expectations::ExpectationNotMetError
- RSpec::Expectations::ExpectationTarget
- RSpec::Expectations::NegativeExpectationHandler
- RSpec::Expectations::PositiveExpectationHandler
- RSpec::Expectations::Syntax
- RSpec::Expectations::Syntax::ExpectExpressionGenerator
- RSpec::Expectations::Syntax::ShouldExpressionGenerator
- RSpec::Expectations::Version
- RSpec::Matchers
- RSpec::Matchers::BuiltIn
- RSpec::Matchers::BuiltIn::BaseMatcher
- RSpec::Matchers::BuiltIn::Be
- RSpec::Matchers::BuiltIn::BeAKindOf
- RSpec::Matchers::BuiltIn::BeAnInstanceOf
- RSpec::Matchers::BuiltIn::BeComparedTo
- RSpec::Matchers::BuiltIn::BeFalse
- RSpec::Matchers::BuiltIn::BeHelpers
- RSpec::Matchers::BuiltIn::BeNil
- RSpec::Matchers::BuiltIn::BePredicate
- RSpec::Matchers::BuiltIn::BeTrue
- RSpec::Matchers::BuiltIn::BeWithin
- RSpec::Matchers::BuiltIn::Change
- RSpec::Matchers::BuiltIn::Cover
- RSpec::Matchers::BuiltIn::EndWith
- RSpec::Matchers::BuiltIn::Eq
- RSpec::Matchers::BuiltIn::Eql
- RSpec::Matchers::BuiltIn::Equal
- RSpec::Matchers::BuiltIn::Exist
- RSpec::Matchers::BuiltIn::Has
- RSpec::Matchers::BuiltIn::Have
- RSpec::Matchers::BuiltIn::Include
- RSpec::Matchers::BuiltIn::Match
- RSpec::Matchers::BuiltIn::MatchArray
- RSpec::Matchers::BuiltIn::NegativeOperatorMatcher
- RSpec::Matchers::BuiltIn::PositiveOperatorMatcher
- RSpec::Matchers::BuiltIn::RaiseError
- RSpec::Matchers::BuiltIn::RespondTo
- RSpec::Matchers::BuiltIn::Satisfy
- RSpec::Matchers::BuiltIn::StartAndEndWith
- RSpec::Matchers::BuiltIn::StartWith
- RSpec::Matchers::BuiltIn::ThrowSymbol
- RSpec::Matchers::BuiltIn::YieldControl
- RSpec::Matchers::BuiltIn::YieldProbe
- RSpec::Matchers::BuiltIn::YieldSuccessiveArgs
- RSpec::Matchers::BuiltIn::YieldWithArgs
- RSpec::Matchers::BuiltIn::YieldWithNoArgs
- RSpec::Matchers::Configuration
- RSpec::Matchers::DSL
- RSpec::Matchers::DSL::Matcher
- RSpec::Matchers::Extensions
- RSpec::Matchers::Extensions::InstanceEvalWithArgs
- RSpec::Matchers::OperatorMatcher
- RSpec::Matchers::Pretty
- Array

No matching classes.

# RSpec::Expectations

RSpec::Expectations adds two instance methods to every object:

    should(matcher=nil)
    should_not(matcher=nil)

Both methods take an optional matcher object (See [RSpec::Matchers](../RSpec/Matchers)). When `should` is invoked with a matcher, it turns around and calls `matcher.matches?(self)`. For example, in the expression:

    order.total.should eq(Money.new(5.55, :USD))

the `should` method invokes the equivalent of `eq.matches?(order.total)`. If `matches?` returns true, the expectation is met and execution continues. If `false`, then the spec fails with the message returned by `eq.failure_message_for_should`.

Given the expression:

    order.entries.should_not include(entry)

the `should_not` method invokes the equivalent of `include.matches?(order.entries)`, but it interprets `false` as success, and `true` as a failure, using the message generated by `eq.failure_message_for_should_not`.

rspec-expectations ships with a standard set of useful matchers, and writing your own matchers is quite simple.

See [RSpec::Matchers](../RSpec/Matchers) for more information about the built-in matchers that ship with rspec-expectations, and how to write your own custom matchers.

### Public Class Methods

differ() click to toggle source

@private

    # File lib/rspec/expectations/fail_with.rb, line 5
    def differ
      @differ ||= Differ.new
    end

fail_with(message, expected=nil, actual=nil) click to toggle source

Raises an RSpec::Expectations::ExpectationNotMetError with message. @param [String] message @param [Object] expected @param [Object] actual

Adds a diff to the failure message when `expected` and `actual` are both present.

    # File lib/rspec/expectations/fail_with.rb, line 16
    def fail_with(message, expected=nil, actual=nil)
      if !message
        raise ArgumentError, "Failure message is nil. Does your matcher define the " +
                             "appropriate failure_message_for_* method to return a string?"
      end

      if actual && expected
        if all_strings?(actual, expected)
          if any_multiline_strings?(actual, expected)
            message << "\nDiff:" << differ.diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
          end
        elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
          message << "\nDiff:" << differ.diff_as_object(actual, expected)
        end
      end

      raise(RSpec::Expectations::ExpectationNotMetError.new(message))
    end

### Public Instance Methods

differ=(ignore) click to toggle source

@deprecated (no replacement)

    # File lib/rspec/expectations/extensions/object.rb, line 23
    def differ=(ignore)
      RSpec.deprecate("RSpec::Expectations.differ=(differ)")
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.