Skip to content

Instantly share code, notes, and snippets.

@allardhoeve
Last active December 29, 2015 17:19
Show Gist options
  • Save allardhoeve/7702807 to your computer and use it in GitHub Desktop.
Save allardhoeve/7702807 to your computer and use it in GitHub Desktop.
class TestNamedTemporaryFile(TestCase):
def setUp(self):
# set up the mock
# you could use ByteTestCase when you are at byte.nl ;)
mockobj = mock.Mock()
patcher = mock.patch("tempfile.NamedTemporaryFile", mockobj)
self.addCleanup(patcher.stop)
patcher.start()
def enter(*args):
return self.mock_tempfile
def exit(*args):
pass
# set exit and enter on mocked instance
self.mock_tempfile = mockobj.return_value
self.mock_tempfile.__exit__ = exit
self.mock_tempfile.__enter__ = enter
self.mock_tempfile.name = "/tmp/mytempfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment