Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>What does <code>expect { |b| 5.tap(&amp;b) }.to yield_control</code> do?</strong> </p> <p>It is expecting that the code given in the block invokes <code>yield</code> statement. </p> <p>The code given in the block (viz. <code>{ |b| 5.tap(&amp;b) }</code>) calls the <a href="http://ruby-doc.org/core-1.9.3/Object.html#method-i-tap" rel="nofollow">ruby 1.9 tap method</a> which has a <code>yield</code> statement within it's implementation. </p> <p>So the statement is effectively asserting that ruby 1.9's tap method has a <code>yield</code> statement. :-)</p> <p>To better understand this statement, try the following code example:</p> <p><code>order.rb</code> file</p> <pre><code>class Order end </code></pre> <p><code>order_spec.rb</code> file</p> <pre><code>require 'rspec-expectations' require './order' describe Order do it "should yield regardless of yielded args" do expect { |b| 5.tap(&amp;b);puts "The value of b is #{b.inspect}" }.to yield_control end end </code></pre> <p>Output from executing the spec:</p> <pre><code>$ rspec yield_spec.rb Order The value of b is #&lt;RSpec::Matchers::BuiltIn::YieldProbe:0x000001008745f0 @used=true, @num_yields=1, @yielded_args=[[5]]&gt; should yield regardless of yielded args Finished in 0.01028 seconds 1 example, 0 failures </code></pre> <p>To understand the other lines in the yielding section, similarly include the <code>expect</code> statements in the spec file as follows:</p> <pre><code> it "should yield with no args" do expect { |b| Order.yield_if_true(true, &amp;b) }.to yield_with_no_args end it "should yield with 5 args" do expect { |b| 5.tap(&amp;b) }.to yield_with_args(5) end it "should yield with integer args" do expect { |b| 5.tap(&amp;b) }.to yield_with_args(Fixnum) end it "should yield with string args" do expect { |b| "a string".tap(&amp;b) }.to yield_with_args(/str/) end it "should yield 1,2,3 successively" do expect { |b| [1, 2, 3].each(&amp;b) }.to yield_successive_args(1, 2, 3) end it "should yield ([:a,1], [:b,2]) successively" do expect { |b| { :a =&gt; 1, :b =&gt; 2 }.each(&amp;b) }.to yield_successive_args([:a, 1], [:b, 2]) end </code></pre> <p>Note: The <code>yield_if_true</code> method used in the second line seems to be removed from where it was originally defined; I got it go work by adding it into the Order class as follows:</p> <pre><code>class Order def self.yield_if_true(flag) yield if flag end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload