diff --git a/nova/test.py b/nova/test.py index 3dbfe764872f..cffaed044e9b 100644 --- a/nova/test.py +++ b/nova/test.py @@ -256,6 +256,18 @@ class TestCase(testtools.TestCase): if key != 'id': del self.__dict__[key] + def stub_out(self, old, new): + """Replace a function for the duration of the test. + + Use the monkey patch fixture to replace a function for the + duration of a test. Useful when you want to provide fake + methods instead of mocks during testing. + + This should be used instead of self.stubs.Set (which is based + on mox) going forward. + """ + self.useFixture(fixtures.MonkeyPatch(old, new)) + def flags(self, **kw): """Override flag variables for a test.""" group = kw.pop('group', None) diff --git a/nova/tests/functional/api_sample_tests/test_services.py b/nova/tests/functional/api_sample_tests/test_services.py index 15128204c3cd..a12e04b9b5a8 100644 --- a/nova/tests/functional/api_sample_tests/test_services.py +++ b/nova/tests/functional/api_sample_tests/test_services.py @@ -16,7 +16,6 @@ from oslo_config import cfg from oslo_utils import timeutils -from nova import db from nova.tests.functional.api_sample_tests import api_sample_base from nova.tests.unit.api.openstack.compute import test_services @@ -44,19 +43,16 @@ class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21): def setUp(self): super(ServicesJsonTest, self).setUp() - self.stubs.Set(db, "service_get_all", - test_services.fake_db_api_service_get_all) - self.stubs.Set(timeutils, "utcnow", test_services.fake_utcnow) - self.stubs.Set(timeutils, "utcnow_ts", - test_services.fake_utcnow_ts) - self.stubs.Set(db, "service_get_by_host_and_binary", - test_services.fake_service_get_by_host_binary) - self.stubs.Set(db, "service_update", - test_services.fake_service_update) - - def tearDown(self): - super(ServicesJsonTest, self).tearDown() - timeutils.clear_time_override() + self.stub_out("nova.db.service_get_all", + test_services.fake_db_api_service_get_all) + self.stub_out("oslo_utils.timeutils.utcnow", test_services.fake_utcnow) + self.stub_out("oslo_utils.timeutils.utcnow_ts", + test_services.fake_utcnow_ts) + self.stub_out("nova.db.service_get_by_host_and_binary", + test_services.fake_service_get_by_host_binary) + self.stub_out("nova.db.service_update", + test_services.fake_service_update) + self.addCleanup(timeutils.clear_time_override) def test_services_list(self): """Return a list of all agent builds."""