title: Stub a CSV file in Ruby Test::Unit
body: <p>So I have a Ruby script which parses a CSV file as a list of rules and does some processing and returns an array of hashes as processed data. </p>
  
  <p>CSV looks like this:</p>
  
  <pre><code>id_number,rule,notes
  1,"dummy_rule","necessary"
  2,"sample_rule","optional"
  </code></pre>
  
  <p>The parsing of CSV looks like this:</p>
  
  <pre><code>def parse_csv(file_name)
    filtered_data = []
    CSV.foreach(file_name, headers: true) do |row|
      filtered_data &lt;&lt; row # some processing
    end
    filtered_data
  end
  </code></pre>
  
  <p>Now I am wondering if it is possible to stub/mock an actual CSV file for unit testing in such a way that I could pass a "filename" into this function. I could make a CSV object and use the generate function but then the actual filename would not be possible.</p>
  
  <pre><code>def test_parse_csv
     expected_result = ["id_number"=&gt;1, "rule"=&gt;"dummy_rule", "notes"=&gt;"optional"}]
     # stub/mock csv with filename: say "rules.csv"
     assert.equal(expected_result, parse_csv(file_name))
  end
  </code></pre>
  
  <p>I use Test::Ruby</p>
  
  <p>I also found a ruby gem library called mocha but I don't know how this works for CSVs</p>
  
  <p><a href="https://github.com/freerange/mocha" rel="nofollow noreferrer">https://github.com/freerange/mocha</a></p>
  
  <p>Any thoughts are welcome!</p>