Skip to content

Instantly share code, notes, and snippets.

@msmhrt
Created March 25, 2012 04:12
Show Gist options
  • Save msmhrt/2191333 to your computer and use it in GitHub Desktop.
Save msmhrt/2191333 to your computer and use it in GitHub Desktop.
side_effect = lambda *args: call(*args)
#!/usr/bin/env python3.2
import unittest
from mock import call, patch
class Spam:
def __init__(self, *args):
pass
class Ham:
def get_spams(self, args_list):
spams = [Spam(*args) for args in args_list]
return spams
class TestHam(unittest.TestCase):
@patch('__main__.Spam')
def test_get_spams(self, mock_spam):
mock_spam.side_effect = lambda *args: call(*args)
args_list = [(1,), ('2',), (3, '4')]
spams = Ham().get_spams(args_list)
expected_calls = [call(*args) for args in args_list]
self.assertEqual(mock_spam.mock_calls, expected_calls)
expected_spams = expected_calls
self.assertEqual(spams, expected_spams)
if __name__ == '__main__': # pragma: no cover
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment