def mock_wrapper(mock = None, proxied = None): """ return a mock wrapper for the given silent mock, delegating to `proxied` when a given call is not set to be intercepted by the mock. You can use the mock wrapper to set expectations or get invocation details for a silent mock """ if mock is None: mock = raw_mock() if (not isinstance(mock, SilentMock)) and proxied is None: # make mock_wrapper(real_obj) act like mock_wrapper(None, proxied=real_obj) proxied = mock mock = raw_mock() return MockWrapper(mock, proxied)
def mock(*args): """ return a mock wrapper for the given silent mock, delegating to `proxied` when a given call is not set to be intercepted by the mock. You can use the mock wrapper to set expectations or get invocation details for a silent mock usage: mock("name of mock"), or mock(raw_mock_to_wrap), or mock(proxied_object), or mock(raw, proxied) """ raw, name, proxied = None, None, None if len(args) == 1: arg = args[0] if isinstance(arg, str): name = arg elif isinstance(arg, SilentMock): raw = arg else: proxied = arg elif len(args) == 2: raw, proxied = args elif len(args) == 0: # this is ok too pass else: raise TypeError("mock takes zero, one, or two arguments - got %s" % (len(args),)) if raw is None: raw = raw_mock() return MockWrapper(raw, proxied, name)
def _make_mock_if_required(self, name, in_dict=False): if name not in self._children: real_child = self._backup_child(name, in_dict) new_child = MockWrapper(raw_mock(name=name), proxied=real_child) self._child_store(in_dict)[name] = new_child # insert its SilentMock into the parent self._insertion_func(in_dict)(self._parent, name, new_child._mock) return self._child_store(in_dict)[name]
def _make_mock_if_required(self, name, in_dict=False): if name not in self._children: target = self._get_target_for_method(name, in_dict) real_child = self._backup_child(name, in_dict) new_child = MockWrapper(raw_mock(name=name), proxied=real_child) self._child_store(in_dict)[name] = new_child self._insertion_func(in_dict)(target, name, new_child._mock) return self._child_store(in_dict)[name]
def __init__(self, wrapped_mock = None, proxied = None): if self.__class__._all_expectations is None: raise RuntimeError(("%s._setup has not been called. " + "Make sure you are inheriting from mock.TestCase, " + "not unittest.TestCase") % (self.__class__.__name__,)) if wrapped_mock is None: wrapped_mock = raw_mock() if not isinstance(wrapped_mock, SilentMock): raise TypeError("expected SilentMock, got %s" % (wrapped_mock.__class__.__name__,)) self._real_set(_mock = wrapped_mock) self._proxied = proxied
def with_methods(self, *methods, **kwmethods): self._with_children(*methods) for key in kwmethods: kwmethods[key] = raw_mock(return_value = kwmethods[key]) return self.with_children(**kwmethods)