Class: RSpec::Matchers::BuiltIn::YieldProbe

### Home Classes Methods

### In Files

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

### Parent

Object

### Methods

- ::new
- ::probe
- #assert_used!
- #assert_valid_expect_block!
- #has_block?
- #single_yield_args
- #successive_yield_args
- #to_proc
- #yielded_once?

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Expectations
- RSpec::Expectations::BlockExpectationTarget
- RSpec::Expectations::Configuration
- RSpec::Expectations::ExpectationHelper
- RSpec::Expectations::ExpectationNotMetError
- RSpec::Expectations::ExpectationTarget
- RSpec::Expectations::LegacyMacherAdapter
- RSpec::Expectations::LegacyMatcherAdapter
- RSpec::Expectations::LegacyMatcherAdapter::RSpec1
- RSpec::Expectations::LegacyMatcherAdapter::RSpec2
- RSpec::Expectations::NegativeExpectationHandler
- RSpec::Expectations::PositiveExpectationHandler
- RSpec::Expectations::Syntax
- RSpec::Expectations::Version
- RSpec::Matchers
- RSpec::Matchers::AliasedMatcher
- RSpec::Matchers::AliasedMatcherWithOperatorSupport
- RSpec::Matchers::AliasedNegatedMatcher
- RSpec::Matchers::BuiltIn
- RSpec::Matchers::BuiltIn::All
- RSpec::Matchers::BuiltIn::BaseMatcher
- RSpec::Matchers::BuiltIn::BaseMatcher::DefaultFailureMessages
- RSpec::Matchers::BuiltIn::Be
- RSpec::Matchers::BuiltIn::BeAKindOf
- RSpec::Matchers::BuiltIn::BeAnInstanceOf
- RSpec::Matchers::BuiltIn::BeBetween
- RSpec::Matchers::BuiltIn::BeComparedTo
- RSpec::Matchers::BuiltIn::BeFalsey
- RSpec::Matchers::BuiltIn::BeHelpers
- RSpec::Matchers::BuiltIn::BeNil
- RSpec::Matchers::BuiltIn::BePredicate
- RSpec::Matchers::BuiltIn::BeTruthy
- RSpec::Matchers::BuiltIn::BeWithin
- RSpec::Matchers::BuiltIn::CaptureStderr
- RSpec::Matchers::BuiltIn::CaptureStdout
- RSpec::Matchers::BuiltIn::CaptureStreamToTempfile
- RSpec::Matchers::BuiltIn::Change
- RSpec::Matchers::BuiltIn::ChangeDetails
- RSpec::Matchers::BuiltIn::ChangeFromValue
- RSpec::Matchers::BuiltIn::ChangeRelatively
- RSpec::Matchers::BuiltIn::ChangeToValue
- RSpec::Matchers::BuiltIn::Compound
- RSpec::Matchers::BuiltIn::Compound::And
- RSpec::Matchers::BuiltIn::Compound::NestedEvaluator
- RSpec::Matchers::BuiltIn::Compound::Or
- RSpec::Matchers::BuiltIn::Compound::SequentialEvaluator
- RSpec::Matchers::BuiltIn::ContainExactly
- RSpec::Matchers::BuiltIn::ContainExactly::PairingsMaximizer
- 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::Exist::ExistenceTest
- RSpec::Matchers::BuiltIn::Has
- RSpec::Matchers::BuiltIn::HaveAttributes
- RSpec::Matchers::BuiltIn::Include
- RSpec::Matchers::BuiltIn::Match
- RSpec::Matchers::BuiltIn::NegativeOperatorMatcher
- RSpec::Matchers::BuiltIn::NullCapture
- RSpec::Matchers::BuiltIn::OperatorMatcher
- RSpec::Matchers::BuiltIn::Output
- RSpec::Matchers::BuiltIn::PositiveOperatorMatcher
- RSpec::Matchers::BuiltIn::RaiseError
- RSpec::Matchers::BuiltIn::RespondTo
- RSpec::Matchers::BuiltIn::Satisfy
- RSpec::Matchers::BuiltIn::SpecificValuesChange
- RSpec::Matchers::BuiltIn::StartAndEndWith
- RSpec::Matchers::BuiltIn::StartOrEndWith
- 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::Composable
- RSpec::Matchers::DSL
- RSpec::Matchers::DSL::DefaultImplementations
- RSpec::Matchers::DSL::Macros
- RSpec::Matchers::DSL::Macros::Deprecated
- RSpec::Matchers::DSL::Matcher
- RSpec::Matchers::ExpectedsForMultipleDiffs
- RSpec::Matchers::MatcherDelegator
- RSpec::Matchers::MatcherProtocol
- RSpec::Matchers::Pretty
- BasicObject
- Object

No matching classes.

# RSpec::Matchers::BuiltIn::YieldProbe

@private Object that is yielded to `expect` when one of the yield matchers is used. Provides information about the yield behavior of the object-under-test.

### Attributes

num_yields[RW]
yielded_args[RW]

### Public Class Methods

new(block) click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 22
    def initialize(block)
      @block = block
      @used = false
      self.num_yields, self.yielded_args = 0, []
    end

probe(block) click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 11
    def self.probe(block)
      probe = new(block)
      return probe unless probe.has_block?
      probe.assert_valid_expect_block!
      block.call(probe)
      probe.assert_used!
      probe
    end

### Public Instance Methods

assert_used!() click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 64
    def assert_used!
      return if @used
      raise "You must pass the argument yielded to your expect block on "                  "to the method-under-test as a block. It acts as a probe that "                  "allows the matcher to detect whether or not the method-under-test "                  "yields, and, if so, how many times, and what the yielded arguments "                  "are."
    end

assert_valid_expect_block!() click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 74
    def assert_valid_expect_block!
      block_signature = RSpec::Support::BlockSignature.new(@block)
      return if RSpec::Support::StrictSignatureVerifier.new(block_signature, [self]).valid?
      raise "Your expect block must accept an argument to be used with this "                    "matcher. Pass the argument as a block on to the method you are testing."
    end

has_block?() click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 28
    def has_block?
      Proc === @block
    end

single_yield_args() click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 43
    def single_yield_args
      yielded_args.first
    end

successive_yield_args() click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 58
    def successive_yield_args
      yielded_args.map do |arg_array|
        arg_array.size == 1 ? arg_array.first : arg_array
      end
    end

to_proc() click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 32
    def to_proc
      @used = true

      probe = self
      Proc.new do |*args|
        probe.num_yields += 1
        probe.yielded_args << args
        nil # to indicate the block does not return a meaningful value
      end
    end

yielded_once?(matcher_name) click to toggle source

    # File lib/rspec/matchers/built_in/yield.rb, line 47
    def yielded_once?(matcher_name)
      case num_yields
      when 1 then true
      when 0 then false
      else
        raise "The #{matcher_name} matcher is not designed to be used with a "                    "method that yields multiple times. Use the yield_successive_args "                    "matcher for that case."
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.