title: Stubbing File.open with Rspec
body: <p>I'm attempting to stub File.open in order to test a method I have that reads a CSV file.</p>
  
  <p>Here's the model:</p>
  
  <pre><code>class BatchTask
    def import(filename)
      CSV.read(filename, :row_sep =&gt; "\r", :col_sep =&gt; ",")
    end
  end
  </code></pre>
  
  <p>Here's the spec code:</p>
  
  <pre><code>let(:data) { "title\tsurname\tfirstname\rtitle2\tsurname2\tfirstname2\r"}
  let(:result) {[["title","surname","firstname"],["title2","surname2","firstname2"]] }
  
  it "should parse file contents and return a result" do
    File.stub(:open).with("file_name","rb") { StringIO.new(data) }
    person.import("file_name").should == result
  end
  </code></pre>
  
  <p>However, when I attempt to do this I get (stacktrace):</p>
  
  <pre><code>Errno::ENOENT in 'BatchTask should parse file contents and return a result'
  No such file or directory - file_name
  /Users/me/app/models/batch_task.rb:4:in `import'
  ./spec/models/batch_task_spec.rb:10:
  
  Finished in 0.006032 seconds
  </code></pre>
  
  <p>I've been banging my head against this one and can't figure out what I'm doing wrong. Any help would be greatly appreciated!</p>