title: Writing a csv temporary file using tempfile
body: <p>So I'm writing a test for this class (edited to be more clear):</p>
  
  <pre><code>class SpreadSheet(object):
      '''awesome docstring'''
      def __init__(self, filename):
          self.filename = filename
          self.table = []
          self.headers = []
  
          with open(self.filename) as csvfile:
              filereader = reader(csvfile, delimiter=',')
              for row in filereader:
                  self.table.append(row)
  
      def create_headers(self, populations):
          ...code...
  
      def lookup_header(self, ltr):
          ...code...
  
      def write_header(self, targetfile):
          ...code...
  </code></pre>
  
  <p>that so far looks like this:</p>
  
  <pre><code>class TestSpreadSheet(unittest.TestCase):
      @contextmanager
      def make_fake_csv(self, data):
          self.fake_namefile = tempfile.NamedTemporaryFile(delete=False)
          with open(self.fake_namefile, 'w') as fake_csv:
              fake_writer = csv.writer(fake_csv)
              fake_writer.writerows(data)
          yield self.fake_namefile.name
          os.unlink(self.fake_namefile.name)
  
      def setUp(self):
          self.headers = []
          self.table = [
              ['Col1', 'Col2', 'Col3', 'Col4', 'Col5', 'Col6', 'Col7', 'Col8'],
              ['val1', 'val2', 'val3', 'val4', 'val5', 'val6', 'val7', 'val8'],
              ['val1', 'val2', 'val3', 'val4', 'val5', 'val6', 'val7', 'val8'],
              ['val1', 'val2', 'val3', 'val4', 'val5', 'val6', 'val7', 'val8']]
  
      def test___init__(self):
          with self.make_fake_csv(self.table) as temp_csv:
              spread_sheet = SpreadSheet(temp_csv)
              self.assertEqual(
                  self.table, spread_sheet.table)
  
      ...tests for other functions...
  </code></pre>
  
  <p>And I get this error:</p>
  
  <pre><code>in make_fake_csv
  with open(self.fake_namefile, 'w') as fake_csv:
  TypeError: coercing to Unicode: need string or buffer, instance found
  </code></pre>
  
  <p>I've scoured many other topics like this that point to the use of <code>tempfile</code> to make a named object or something that can actually be called using <code>with open...</code>. And while I did actually get that to work, my issues was when I tried to use the <code>csv</code> package to format my <code>self.table</code> for me into a csv formatted raw "string" (like the raw input of a csv file in other words). </p>
  
  <p>Any pointers on how I can test this differently or make the current code work? Again I'm trying to:</p>
  
  <ol>
  <li><p>figure out how to use <code>csv</code> to do all the formatting heavy-lifting to load up a fake csv file from my <code>self.table</code> so that I don't have to make a huge string formatting expression</p></li>
  <li><p>make sure that the fake file works with <code>with open</code> as used in my original class <code>SpreadSheet</code> when the test is run</p></li>
  <li><p>can be used further to run the test of the other functions because they too need to instantiate <code>SpreadSheet</code> with a file in order to perform their functions.</p></li>
  </ol>
  
  <p>And as a side question, is it "leaner" to make a fake "memory" file to do things like this (this is what I'm attempting above) or is is just simpler to make an actual temporary file on the disk and load it up during testing and use a <code>tearDown()</code> function to delete it?</p>