Skip to content

Instantly share code, notes, and snippets.

@trecouvr
Last active November 12, 2015 21:46
Show Gist options
  • Save trecouvr/9782603 to your computer and use it in GitHub Desktop.
Save trecouvr/9782603 to your computer and use it in GitHub Desktop.
import asyncio
import unittest
import unittest.mock
class MyClass:
@asyncio.coroutine
def coro1(self):
yield from self.coro2()
@asyncio.coroutine
def coro2(self):
return True
class MockTestCase(unittest.TestCase):
def test_call_coro2(self):
loop = asyncio.get_event_loop()
m = MyClass()
mm = unittest.mock.MagicMock()
mm.iter.return_value = 'iterable'
m.coro2 = unittest.mock.Mock(return_value=mm)
coro = m.coro1()
result = loop.run_until_complete(coro)
self.assertEqual(1, mm.__iter__.call_count)
@alanc10n
Copy link

When I run this, result is None; I'd expect it to be 'iterable' since that's what's being assigned for the return_value. Is there some way to actually set the return value for the mocked method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment