Class: RSpec::Matchers::BuiltIn::YieldProbe
- Inherits:
-
Object
- Object
- RSpec::Matchers::BuiltIn::YieldProbe
- Defined in:
- lib/rspec/matchers/built_in/yield.rb
Instance Attribute Summary collapse
-
#num_yields ⇒ Object
Returns the value of attribute num_yields.
-
#yielded_args ⇒ Object
Returns the value of attribute yielded_args.
Class Method Summary collapse
Instance Method Summary collapse
- #assert_used! ⇒ Object
-
#initialize ⇒ YieldProbe
constructor
A new instance of YieldProbe.
- #single_yield_args ⇒ Object
- #successive_yield_args ⇒ Object
- #to_proc ⇒ Object
- #yielded_once?(matcher_name) ⇒ Boolean
Constructor Details
#initialize ⇒ YieldProbe
Returns a new instance of YieldProbe.
15 16 17 18 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 15 def initialize @used = false self.num_yields, self.yielded_args = 0, [] end |
Instance Attribute Details
#num_yields ⇒ Object
Returns the value of attribute num_yields.
13 14 15 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 13 def num_yields @num_yields end |
#yielded_args ⇒ Object
Returns the value of attribute yielded_args.
13 14 15 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 13 def yielded_args @yielded_args end |
Class Method Details
.assert_valid_expect_block!(block) ⇒ Object
60 61 62 63 64 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 60 def self.assert_valid_expect_block!(block) return if block.arity == 1 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(block) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 5 def self.probe(block) probe = new assert_valid_expect_block!(block) block.call(probe) probe.assert_used! probe end |
Instance Method Details
#assert_used! ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 51 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 |
#single_yield_args ⇒ Object
30 31 32 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 30 def single_yield_args yielded_args.first end |
#successive_yield_args ⇒ Object
45 46 47 48 49 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 45 def successive_yield_args yielded_args.map do |arg_array| arg_array.size == 1 ? arg_array.first : arg_array end end |
#to_proc ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 20 def to_proc @used = true probe = self Proc.new do |*args| probe.num_yields += 1 probe.yielded_args << args end end |
#yielded_once?(matcher_name) ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rspec/matchers/built_in/yield.rb', line 34 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 |