Stop using non-existent method of Mock

There is no method called_once_with() in Mock object.
Use assert_called_once_with() instead.

Change-Id: I263c407ba2a82f53aace5f71cb27ad8974335e24
Closes-Bug: #1544522
This commit is contained in:
Bo Wang 2016-02-15 23:05:20 +08:00
parent 33eec87d78
commit fd0ed209d2
4 changed files with 16 additions and 6 deletions

View File

@ -115,12 +115,14 @@ def check_assert_called_once_with(logical_line, filename):
# assert_called_once
# assertCalledOnceWith
# assert_has_called
# called_once_with
if 'neutron/tests/' in filename:
if '.assert_called_once_with(' in logical_line:
return
uncased_line = logical_line.lower().replace('_', '')
if '.assertcalledonce' in uncased_line:
check_calls = ['.assertcalledonce', '.calledoncewith']
if any(x for x in check_calls if x in uncased_line):
msg = ("N322: Possible use of no-op mock method. "
"please use assert_called_once_with.")
yield (0, msg)

View File

@ -1490,8 +1490,8 @@ class TestDeviceManager(base.BaseTestCase):
uuid5.return_value = '1ae5f96c-c527-5079-82ea-371a01645457'
dh = dhcp.DeviceManager(cfg.CONF, None)
uuid5.called_once_with(uuid.NAMESPACE_DNS, cfg.CONF.host)
self.assertEqual(dh.get_device_id(fake_net), expected)
uuid5.assert_called_once_with(uuid.NAMESPACE_DNS, cfg.CONF.host)
def test_update(self):
# Try with namespaces and no metadata network

View File

@ -205,9 +205,9 @@ class TestMetadataProxyHandlerCache(TestMetadataProxyHandlerBase):
) as mock_get_router_networks:
ports = self.handler._get_ports(remote_address,
router_id=router_id)
mock_get_router_networks.called_once_with(router_id)
mock_get_ip_addr.assert_called_once_with(remote_address, networks)
self.assertEqual(expected, ports)
mock_get_router_networks.assert_called_once_with(router_id)
mock_get_ip_addr.assert_called_once_with(remote_address, networks)
self.assertEqual(expected, ports)
def test_get_ports_no_id(self):
self.assertRaises(TypeError, self.handler._get_ports, 'remote_address')

View File

@ -103,6 +103,11 @@ class HackingTestCase(base.BaseTestCase):
mock.method.assertCalledOnceWith()
"""
fail_code3 = """
mock = Mock()
mock.method(1, 2, 3, test='wow')
mock.method.called_once_with()
"""
fail_code4 = """
mock = Mock()
mock.method(1, 2, 3, test='wow')
mock.method.assert_has_called()
@ -123,11 +128,14 @@ class HackingTestCase(base.BaseTestCase):
self.assertEqual(
1, len(list(checks.check_assert_called_once_with(fail_code2,
"neutron/tests/test_assert.py"))))
self.assertEqual(
1, len(list(checks.check_assert_called_once_with(fail_code3,
"neutron/tests/test_assert.py"))))
self.assertEqual(
0, len(list(checks.check_assert_called_once_with(pass_code,
"neutron/tests/test_assert.py"))))
self.assertEqual(
1, len(list(checks.check_assert_called_once_with(fail_code3,
1, len(list(checks.check_assert_called_once_with(fail_code4,
"neutron/tests/test_assert.py"))))
self.assertEqual(
0, len(list(checks.check_assert_called_once_with(pass_code2,