Class: Mocha::Cardinality

### Home Classes Methods

### In Files

- lib/mocha/cardinality.rb

### Parent

Object

### Methods

- ::at_least
- ::at_most
- ::exactly
- ::new
- ::times
- #allowed_any_number_of_times?
- #infinite?
- #invocations_allowed?
- #mocha_inspect
- #needs_verifying?
- #satisfied?
- #times
- #used?
- #verified?

### Class/Module Index

Quicksearch

- Mocha
- Mocha::API
- Mocha::AnyInstanceMethod
- Mocha::AnyInstanceReceiver
- Mocha::ArgumentIterator
- Mocha::ArrayMethods
- Mocha::BacktraceFilter
- Mocha::Cardinality
- Mocha::Central
- Mocha::ChangeStateSideEffect
- Mocha::ClassMethod
- Mocha::ClassMethods
- Mocha::ClassMethods::AnyInstance
- Mocha::Configuration
- Mocha::DateMethods
- Mocha::Debug
- Mocha::DefaultName
- Mocha::DefaultReceiver
- Mocha::Deprecation
- Mocha::Detection
- Mocha::Detection::MiniTest
- Mocha::Detection::TestUnit
- Mocha::ExceptionRaiser
- Mocha::Expectation
- Mocha::ExpectationError
- Mocha::ExpectationErrorFactory
- Mocha::ExpectationList
- Mocha::HashMethods
- Mocha::Hooks
- Mocha::ImpersonatingAnyInstanceName
- Mocha::ImpersonatingName
- Mocha::InStateOrderingConstraint
- Mocha::InstanceMethod
- Mocha::Integration
- Mocha::Integration::AssertionCounter
- Mocha::Integration::MiniTest
- Mocha::Integration::MiniTest::Adapter
- Mocha::Integration::MiniTest::Nothing
- Mocha::Integration::MiniTest::Version13
- Mocha::Integration::MiniTest::Version13::RunMethodPatch
- Mocha::Integration::MiniTest::Version140
- Mocha::Integration::MiniTest::Version140::RunMethodPatch
- Mocha::Integration::MiniTest::Version141
- Mocha::Integration::MiniTest::Version141::RunMethodPatch
- Mocha::Integration::MiniTest::Version142To172
- Mocha::Integration::MiniTest::Version142To172::RunMethodPatch
- Mocha::Integration::MiniTest::Version200
- Mocha::Integration::MiniTest::Version200::RunMethodPatch
- Mocha::Integration::MiniTest::Version201To222
- Mocha::Integration::MiniTest::Version201To222::RunMethodPatch
- Mocha::Integration::MiniTest::Version2110To2111
- Mocha::Integration::MiniTest::Version2110To2111::RunMethodPatch
- Mocha::Integration::MiniTest::Version2112To320
- Mocha::Integration::MiniTest::Version2112To320::RunMethodPatch
- Mocha::Integration::MiniTest::Version230To2101
- Mocha::Integration::MiniTest::Version230To2101::RunMethodPatch
- Mocha::Integration::MonkeyPatcher
- Mocha::Integration::TestUnit
- Mocha::Integration::TestUnit::Adapter
- Mocha::Integration::TestUnit::GemVersion200
- Mocha::Integration::TestUnit::GemVersion200::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion201To202
- Mocha::Integration::TestUnit::GemVersion201To202::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion203To220
- Mocha::Integration::TestUnit::GemVersion203To220::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion230To250
- Mocha::Integration::TestUnit::GemVersion230To250::RunMethodPatch
- Mocha::Integration::TestUnit::Nothing
- Mocha::Integration::TestUnit::RubyVersion185AndBelow
- Mocha::Integration::TestUnit::RubyVersion185AndBelow::RunMethodPatch
- Mocha::Integration::TestUnit::RubyVersion186AndAbove
- Mocha::Integration::TestUnit::RubyVersion186AndAbove::RunMethodPatch
- Mocha::Logger
- Mocha::MethodMatcher
- Mocha::Mock
- Mocha::Mockery
- Mocha::ModuleMethod
- Mocha::ModuleMethods
- Mocha::MultipleYields
- Mocha::Name
- Mocha::NoYields
- Mocha::ObjectMethods
- Mocha::ObjectReceiver
- Mocha::ParameterMatchers
- Mocha::ParameterMatchers::AllOf
- Mocha::ParameterMatchers::AnyOf
- Mocha::ParameterMatchers::AnyParameters
- Mocha::ParameterMatchers::Anything
- Mocha::ParameterMatchers::Base
- Mocha::ParameterMatchers::Equals
- Mocha::ParameterMatchers::HasEntries
- Mocha::ParameterMatchers::HasEntry
- Mocha::ParameterMatchers::HasKey
- Mocha::ParameterMatchers::HasValue
- Mocha::ParameterMatchers::Includes
- Mocha::ParameterMatchers::InstanceOf
- Mocha::ParameterMatchers::IsA
- Mocha::ParameterMatchers::KindOf
- Mocha::ParameterMatchers::Not
- Mocha::ParameterMatchers::Optionally
- Mocha::ParameterMatchers::QueryStringMatches
- Mocha::ParameterMatchers::RegexpMatches
- Mocha::ParameterMatchers::RespondsWith
- Mocha::ParameterMatchers::YamlEquivalent
- Mocha::ParametersMatcher
- Mocha::PrettyParameters
- Mocha::ReturnValues
- Mocha::Sequence
- Mocha::Sequence::InSequenceOrderingConstraint
- Mocha::SingleReturnValue
- Mocha::SingleYield
- Mocha::StateMachine
- Mocha::StateMachine::State
- Mocha::StateMachine::StatePredicate
- Mocha::StringMethods
- Mocha::StubbingError
- Mocha::Thrower
- Mocha::TimeMethods
- Mocha::UnexpectedInvocation
- Mocha::YieldParameters
- Array
- Date
- Hash
- Object
- String
- Time

No matching classes.

# Mocha::Cardinality

### Constants

INFINITY

### Attributes

maximum[R]
required[R]

### Public Class Methods

at_least(count) click to toggle source

    # File lib/mocha/cardinality.rb, line 13
    def at_least(count)
      new(count, INFINITY)
    end

at_most(count) click to toggle source

    # File lib/mocha/cardinality.rb, line 17
    def at_most(count)
      new(0, count)
    end

exactly(count) click to toggle source

    # File lib/mocha/cardinality.rb, line 9
    def exactly(count)
      new(count, count)
    end

new(required, maximum) click to toggle source

    # File lib/mocha/cardinality.rb, line 30
    def initialize(required, maximum)
      @required, @maximum = required, maximum
    end

times(range_or_count) click to toggle source

    # File lib/mocha/cardinality.rb, line 21
    def times(range_or_count)
      case range_or_count
        when Range then new(range_or_count.first, range_or_count.last)
        else new(range_or_count, range_or_count)
      end
    end

### Public Instance Methods

allowed_any_number_of_times?() click to toggle source

    # File lib/mocha/cardinality.rb, line 50
    def allowed_any_number_of_times?
      required == 0 && infinite?(maximum)
    end

invocations_allowed?(invocation_count) click to toggle source

    # File lib/mocha/cardinality.rb, line 34
    def invocations_allowed?(invocation_count)
      invocation_count < maximum
    end

mocha_inspect() click to toggle source

    # File lib/mocha/cardinality.rb, line 58
    def mocha_inspect
      if allowed_any_number_of_times?
        "allowed any number of times"
      else
        if required == 0 && maximum == 0
          "expected never"
        elsif required == maximum
          "expected exactly #{times(required)}"
        elsif infinite?(maximum)
          "expected at least #{times(required)}"
        elsif required == 0
          "expected at most #{times(maximum)}"
        else
          "expected between #{required} and #{times(maximum)}"
        end
      end
    end

needs_verifying?() click to toggle source

    # File lib/mocha/cardinality.rb, line 42
    def needs_verifying?
      !allowed_any_number_of_times?
    end

satisfied?(invocations_so_far) click to toggle source

    # File lib/mocha/cardinality.rb, line 38
    def satisfied?(invocations_so_far)
      invocations_so_far >= required
    end

used?(invocation_count) click to toggle source

    # File lib/mocha/cardinality.rb, line 54
    def used?(invocation_count)
      (invocation_count > 0) || (maximum == 0)
    end

verified?(invocation_count) click to toggle source

    # File lib/mocha/cardinality.rb, line 46
    def verified?(invocation_count)
      (invocation_count >= required) && (invocation_count <= maximum)
    end

### Protected Instance Methods

infinite?(number) click to toggle source

    # File lib/mocha/cardinality.rb, line 89
    def infinite?(number)
      number.respond_to?(:infinite?) && number.infinite?
    end

times(number) click to toggle source

    # File lib/mocha/cardinality.rb, line 80
    def times(number)
      case number
        when 0 then "no times"
        when 1 then "once"
        when 2 then "twice"
        else "#{number} times"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.