Use mock's call assert methods over call_args_list

The way we're checking the value of call_args_list on a mock method is
essentially what assert_called_once_with() since we also know the
method is only called once. In other cases, we can use
assert_has_calls().

I'm slightly nervous of using call_args_list because it's the kind of
thing you could imagine changing in future, even though it's not
marked private.

Change-Id: I7eb3d095f7a1ff9a4abc9e027193d4918d0cda6c
This commit is contained in:
Mark McLoughlin 2014-02-14 11:53:06 +00:00
parent ed2a1545c0
commit 9e47dbe00d
2 changed files with 29 additions and 27 deletions

@ -116,13 +116,14 @@ class TestDispatcher(test_utils.BaseTestCase):
for m in endpoint_methods:
if m == self.endpoints_expect_calls[i]:
method = getattr(endpoints[i], m)
expected = [mock.call({}, msg['publisher_id'],
msg['event_type'],
msg['payload'], {
'timestamp': mock.ANY,
'message_id': mock.ANY
})]
self.assertEqual(method.call_args_list, expected)
method.assert_called_once_with(
{},
msg['publisher_id'],
msg['event_type'],
msg['payload'], {
'timestamp': mock.ANY,
'message_id': mock.ANY
})
else:
self.assertEqual(endpoints[i].call_count, 0)

@ -150,14 +150,14 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
self._stop_listener(listener_thread)
expected = [mock.call({'ctxt': '1'}, 'testpublisher',
'an_event.start1', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY}),
mock.call({'ctxt': '2'}, 'testpublisher',
'an_event.start2', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY})]
self.assertEqual(sorted(endpoint.info.call_args_list), expected)
endpoint.info.assert_has_calls([
mock.call({'ctxt': '1'}, 'testpublisher',
'an_event.start1', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY}),
mock.call({'ctxt': '2'}, 'testpublisher',
'an_event.start2', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY})],
any_order=True)
def test_two_exchanges(self):
transport = messaging.get_transport(self.conf, url='fake:')
@ -192,13 +192,14 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
self._stop_listener(listener_thread)
expected = [mock.call({'ctxt': '1'}, 'testpublisher', 'an_event.start',
'test message exchange1',
{'timestamp': mock.ANY, 'message_id': mock.ANY}),
mock.call({'ctxt': '2'}, 'testpublisher', 'an_event.start',
'test message exchange2',
{'timestamp': mock.ANY, 'message_id': mock.ANY})]
self.assertEqual(sorted(endpoint.info.call_args_list), expected)
endpoint.info.assert_has_calls([
mock.call({'ctxt': '1'}, 'testpublisher', 'an_event.start',
'test message exchange1',
{'timestamp': mock.ANY, 'message_id': mock.ANY}),
mock.call({'ctxt': '2'}, 'testpublisher', 'an_event.start',
'test message exchange2',
{'timestamp': mock.ANY, 'message_id': mock.ANY})],
any_order=True)
def test_two_endpoints(self):
transport = messaging.get_transport(self.conf, url='fake:')
@ -242,8 +243,8 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
self._stop_listener(listener_thread)
expected = [mock.call({}, 'testpublisher', 'an_event.start', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY}),
mock.call({}, 'testpublisher', 'an_event.start', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY})]
self.assertEqual(endpoint.info.call_args_list, expected)
endpoint.info.assert_has_calls([
mock.call({}, 'testpublisher', 'an_event.start', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY}),
mock.call({}, 'testpublisher', 'an_event.start', 'test',
{'timestamp': mock.ANY, 'message_id': mock.ANY})])