]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Remove function replacement with mock patch
authorKevin Benton <blak111@gmail.com>
Fri, 30 May 2014 03:59:00 +0000 (20:59 -0700)
committerKevin Benton <blak111@gmail.com>
Fri, 30 May 2014 03:59:00 +0000 (20:59 -0700)
In the hyperv unit tests, an rpc method is
manually replaced with a MagicMock using setattr.
This prevents it from being cleaned up by mock.patch.stopall.
This patch replaces it with a short-lived patch call
in a with statement.

Change-Id: I7b10a8115c474977e3acd16dc49d6f3ae67b0c3b
Partial-Bug: #1316401

neutron/tests/unit/hyperv/test_hyperv_rpcapi.py

index 2765acf0437efd2c7286a07513882907c3d25ef5..4af19fc546ba4153d4043ec17eaeb497305d1afb 100644 (file)
@@ -42,14 +42,11 @@ class rpcHyperVApiTestCase(base.BaseTestCase):
         if rpc_method == 'cast' and method == 'run_instance':
             kwargs['call'] = False
 
-        rpc_method_mock = mock.Mock()
-        rpc_method_mock.return_value = expected_retval
-        setattr(rpc, rpc_method, rpc_method_mock)
-
-        retval = getattr(rpcapi, method)(ctxt, **kwargs)
+        with mock.patch.object(rpc, rpc_method) as rpc_method_mock:
+            rpc_method_mock.return_value = expected_retval
+            retval = getattr(rpcapi, method)(ctxt, **kwargs)
 
         self.assertEqual(retval, expected_retval)
-
         expected_args = [ctxt, topic, expected_msg]
         for arg, expected_arg in zip(rpc_method_mock.call_args[0],
                                      expected_args):