blob: d7dcc117ba89a28faffa48a8d57a3b3815476cc2 [file] [log] [blame]
Matthew Treinish9b4d5882013-10-24 20:00:11 +00001# Copyright 2010 United States Government as represented by the
2# Administrator of the National Aeronautics and Space Administration.
3# Copyright 2013 Hewlett-Packard Development Company, L.P.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import fixtures
19import mock
20
21
22class PatchObject(fixtures.Fixture):
23 """Deal with code around mock."""
24
Mauro S. M. Rodrigues79905982014-01-13 18:32:46 +000025 def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs):
Matthew Treinish9b4d5882013-10-24 20:00:11 +000026 self.obj = obj
27 self.attr = attr
28 self.kwargs = kwargs
Mauro S. M. Rodrigues79905982014-01-13 18:32:46 +000029 self.new = new
Matthew Treinish9b4d5882013-10-24 20:00:11 +000030
31 def setUp(self):
32 super(PatchObject, self).setUp()
Mauro S. M. Rodrigues79905982014-01-13 18:32:46 +000033 _p = mock.patch.object(self.obj, self.attr, self.new, **self.kwargs)
Matthew Treinish9b4d5882013-10-24 20:00:11 +000034 self.mock = _p.start()
35 self.addCleanup(_p.stop)
36
37
38class Patch(fixtures.Fixture):
39
40 """Deal with code around mock.patch."""
41
42 def __init__(self, obj, **kwargs):
43 self.obj = obj
44 self.kwargs = kwargs
45
46 def setUp(self):
47 super(Patch, self).setUp()
48 _p = mock.patch(self.obj, **self.kwargs)
49 self.mock = _p.start()
50 self.addCleanup(_p.stop)