title: How to test a function which takes a block with rspec
body: <p>I have a function, which accepts a block, opens a file, yields and returns:</p>
  
  <pre><code>def start &amp;block
      .....do some stuff
      File.open("filename", "w") do |f|
          f.write("something")
          ....do some more stuff
          yield
      end
  end
  </code></pre>
  
  <p>I am trying to write a test for it using rspec. How do I stub File.open so that it passed an object f (supplied by me) to the block instead of trying to open an actual file? Something like:</p>
  
  <pre><code>it "should test something" do
      myobject = double("File", {'write' =&gt; true})
      File.should_receive(:open).with(&amp;blk) do |myobject|
          f.should_receive(:write)
          blk.should_receive(:yield) (or somethig like that)
      end
  end
  </code></pre>