Class: RSpec::Matchers::BuiltIn::Have

### Home Classes Methods

### In Files

- lib/rspec/matchers/built_in/have.rb

### Parent

Object

### Methods

- ::new
- #==
- #description
- #determine_collection
- #determine_query_method
- #failure_message_for_should
- #failure_message_for_should_not
- #matches?
- #not_a_collection
- #relativities
- #respond_to?

### 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::Matchers::BuiltIn::Have

### Constants

QUERY_METHODS

### Public Class Methods

new(expected, relativity=:exactly) click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 7
    def initialize(expected, relativity=:exactly)
      @expected = case expected
                  when :no then 0
                  when String then expected.to_i
                  else expected
                  end
      @relativity = relativity
      @actual = @collection_name = @plural_collection_name = nil
    end

### Public Instance Methods

==(collection_or_owner) click to toggle source
Alias for: matches?
description() click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 94
    def description
      "have #{relative_expectation} #{@collection_name}"
    end

determine_collection(collection_or_owner) click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 48
    def determine_collection(collection_or_owner)
      if collection_or_owner.respond_to?(@collection_name)
        collection_or_owner.__send__(@collection_name, *@args, &@block)
      elsif (@plural_collection_name && collection_or_owner.respond_to?(@plural_collection_name))
        collection_or_owner.__send__(@plural_collection_name, *@args, &@block)
      elsif determine_query_method(collection_or_owner)
        collection_or_owner
      else
        collection_or_owner.__send__(@collection_name, *@args, &@block)
      end
    end

determine_query_method(collection) click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 60
    def determine_query_method(collection)
      QUERY_METHODS.detect {|m| collection.respond_to?(m)}
    end

failure_message_for_should() click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 68
    def failure_message_for_should
      "expected #{relative_expectation} #{@collection_name}, got #{@actual}"
    end

failure_message_for_should_not() click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 72
    def failure_message_for_should_not
      if @relativity == :exactly
        return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
      elsif @relativity == :at_most
        return Isn't life confusing enough?Instead of having to figure out the meaning of this:  #{Expectations::Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}We recommend that you use this instead:  #{Expectations::Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")}
      elsif @relativity == :at_least
        return Isn't life confusing enough?Instead of having to figure out the meaning of this:  #{Expectations::Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}We recommend that you use this instead:  #{Expectations::Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
      end
    end

matches?(collection_or_owner) click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 25
    def matches?(collection_or_owner)
      collection = determine_collection(collection_or_owner)
      case collection
      when enumerator_class
        for query_method in QUERY_METHODS
          next unless collection.respond_to?(query_method)
          @actual = collection.__send__(query_method)
          break unless @actual.nil?
        end
        raise not_a_collection if @actual.nil?
      else
        query_method = determine_query_method(collection)
        raise not_a_collection unless query_method
        @actual = collection.__send__(query_method)
      end
      case @relativity
      when :at_least then @actual >= @expected
      when :at_most  then @actual <= @expected
      else                @actual == @expected
      end
    end

Also aliased as: ==
not_a_collection() click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 64
    def not_a_collection
      "expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
    end

relativities() click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 17
    def relativities
      @relativities ||= {
        :exactly => "",
        :at_least => "at least ",
        :at_most => "at most "
      }
    end

respond_to?(m) click to toggle source

    # File lib/rspec/matchers/built_in/have.rb, line 98
    def respond_to?(m)
      @expected.respond_to?(m) || super
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.