Skip to content

Instantly share code, notes, and snippets.

@wilkes
Created October 5, 2010 00:53
Show Gist options
  • Save wilkes/610755 to your computer and use it in GitHub Desktop.
Save wilkes/610755 to your computer and use it in GitHub Desktop.
def copy_to_temp(file_upload):
tmp = tempfile.NamedTemporaryFile(delete=False)
while True:
data = file_upload.read(8192)
tmp.write(data)
if not data:
break
tmp.flush()
return tmp
# I want to assert that
# - NamedTemporaryFile is called with delete=False
# - file_upload.read(8192) is called and returns ''
# - tmp.write is called with ''
# - tmp.flush is called
# - tmp is returned
=== Result
from nose.tools import *
from mock import Mock, patch
@patch('tempfile.NamedTemporaryFile')
def test_copy_to_temp(MockTempFile):
tmp = MockTempFile.return_value
file_upload = Mock()
file_upload.read.return_value = ""
result = copy_to_temp(file_upload)
MockTempFile.assert_called_with(delete=False)
tmp.write.assert_called_with("")
assert tmp.flush.called
assert tmp == result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment