123456789_123456789_123456789_123456789_123456789_

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

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: rspec-expectations/lib/rspec/matchers/built_in/yield.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Method Details

.probe(block, &callback)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 13

def self.probe(block, &callback)
  probe = new(block, &callback)
  return probe unless probe.has_block?
  probe.probe
end

Instance Attribute Details

#has_block?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 29

def has_block?
  Proc === @block
end

#num_yields (rw)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 19

attr_accessor :num_yields, :yielded_args

#yielded_args (rw)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 19

attr_accessor :num_yields, :yielded_args

Instance Method Details

#assert_used!

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 68

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!

:nocov: On 1.8.7, ‘lambda { }.arity` and `lambda { |*a| }.arity` both return -1, so we can’t distinguish between accepting no args and an arg splat. It’s OK to skip, this, though; it just provides a nice error message when the user forgets to accept an arg in their block. They’ll still get the ‘assert_used!` error message from above, which is sufficient.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 91

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

#probe

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 33

def probe
  assert_valid_expect_block!
  @block.call(self)
  assert_used!
  self
end

#single_yield_args

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 53

def single_yield_args
  yielded_args.first
end

#to_proc

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 40

def to_proc
  @used = true

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

#yielded_once?(matcher_name) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/yield.rb', line 57

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