title: RSpec mocking an :each block
body: <p>I want to use RSpec mocks to provide canned input to a block.</p>
  
  <p>Ruby:</p>
  
  <pre><code>class Parser
    attr_accessor :extracted
  
    def parse(fname)
      File.open(fname).each do |line|
        extracted = line if line =~ /^RCS file: (.*),v$/
      end
    end
  end
  </code></pre>
  
  <p>RSpec:</p>
  
  <pre><code>describe Parser
    before do
      @parser = Parser.new
      @lines = mock("lines")
      @lines.stub!(:each)
      File.stub!(:open).and_return(@lines)
    end
  
    it "should extract a filename into extracted" do
      linetext = [ "RCS file: hello,v\n", "bla bla bla\n" ]
  
      # HELP ME HERE ...
      # the :each should be fed with 'linetext'
      @lines.should_receive(:each)
  
      @parser.should_receive('extracted=')
      @parser.parse("somefile.txt")
    end
  end
  </code></pre>
  
  <p>It's a way to test that the internals of the block work correctly by passing fixtured data into it. But I can't figure out how to do the actual feeding with RSpec mocking mechanism.</p>
  
  <p><strong>update:</strong> looks like the problem was not with the linetext, but with the:</p>
  
  <pre><code>@parser.should_receive('extracted=')
  </code></pre>
  
  <p>it's not the way it's called, replacing it in the ruby code with self.extracted= helps a bit, but feels wrong somehow.</p>