mocksignature and tests removed
diff --git a/docs/changelog.txt b/docs/changelog.txt
index c54ba12..90c1ed3 100644
--- a/docs/changelog.txt
+++ b/docs/changelog.txt
@@ -4,10 +4,12 @@
CHANGELOG
=========
-2012/XX/XX Version 0.8.1
+2012/XX/XX Version 1.0.0
------------------------
-* BUGFIX: `create_autospec` works correctly for calling mocked unbound methods
+The standard library version!
+
+* `mocksignature`, along with the `mocksignature` argument to `patch`, removed
2012/02/13 Version 0.8.0
diff --git a/mock.py b/mock.py
index 31a1147..640203b 100644
--- a/mock.py
+++ b/mock.py
@@ -3,7 +3,7 @@
# Copyright (C) 2007-2012 Michael Foord & the mock team
# E-mail: fuzzyman AT voidspace DOT org DOT uk
-# mock 0.9.0
+# mock 1.0
# http://www.voidspace.org.uk/python/mock/
# Released subject to the BSD License
@@ -16,7 +16,6 @@
__all__ = (
'Mock',
'MagicMock',
- 'mocksignature',
'patch',
'sentinel',
'DEFAULT',
@@ -29,7 +28,7 @@
)
-__version__ = '0.9.0alpha1'
+__version__ = '1.0alpha1'
import pprint
@@ -136,43 +135,7 @@
)
-# getsignature and mocksignature heavily "inspired" by
-# the decorator module: http://pypi.python.org/pypi/decorator/
-# by Michele Simionato
-
-def _getsignature(func, skipfirst):
- if inspect is None:
- raise ImportError('inspect module not available')
-
- if inspect.isclass(func):
- func = func.__init__
- # will have a self arg
- skipfirst = True
- elif not (inspect.ismethod(func) or inspect.isfunction(func)):
- func = func.__call__
-
- regargs, varargs, varkwargs, defaults = inspect.getargspec(func)
-
- # instance methods need to lose the self argument
- if getattr(func, self, None) is not None:
- regargs = regargs[1:]
-
- _msg = ("_mock_ is a reserved argument name, can't mock signatures using "
- "_mock_")
- assert '_mock_' not in regargs, _msg
- if varargs is not None:
- assert '_mock_' not in varargs, _msg
- if varkwargs is not None:
- assert '_mock_' not in varkwargs, _msg
- if skipfirst:
- regargs = regargs[1:]
-
- signature = inspect.formatargspec(regargs, varargs, varkwargs, defaults,
- formatvalue=lambda value: "")
- return signature[1:-1], func
-
-
-def _getsignature2(func, skipfirst, instance=False):
+def _getsignature(func, skipfirst, instance=False):
if inspect is None:
raise ImportError('inspect module not available')
@@ -211,7 +174,7 @@
if not _callable(func):
return
- result = _getsignature2(func, skipfirst, instance)
+ result = _getsignature(func, skipfirst, instance)
if result is None:
return
signature, func = result
@@ -271,12 +234,12 @@
def _set_signature(mock, original, instance=False):
# creates a function with signature (*args, **kwargs) that delegates to a
# mock. It still does signature checking by calling a lambda with the same
- # signature as the original. This is effectively mocksignature2.
+ # signature as the original.
if not _callable(original):
return
skipfirst = isinstance(original, ClassTypes)
- result = _getsignature2(original, skipfirst, instance)
+ result = _getsignature(original, skipfirst, instance)
if result is None:
# was a C function (e.g. object().__init__ ) that can't be mocked
return
@@ -301,41 +264,6 @@
return funcopy
-def mocksignature(func, mock=None, skipfirst=False):
- """
- mocksignature(func, mock=None, skipfirst=False)
-
- Create a new function with the same signature as `func` that delegates
- to `mock`. If `skipfirst` is True the first argument is skipped, useful
- for methods where `self` needs to be omitted from the new function.
-
- If you don't pass in a `mock` then one will be created for you.
-
- The mock is set as the `mock` attribute of the returned function for easy
- access.
-
- Functions returned by `mocksignature` have many of the same attributes
- and assert methods as a mock object.
-
- `mocksignature` can also be used with classes. It copies the signature of
- the `__init__` method.
-
- When used with callable objects (instances) it copies the signature of the
- `__call__` method.
- """
- if mock is None:
- mock = Mock()
- signature, func = _getsignature(func, skipfirst)
- src = "lambda %(signature)s: _mock_(%(signature)s)" % {
- 'signature': signature
- }
-
- funcopy = eval(src, dict(_mock_=mock))
- _copy_func_details(func, funcopy)
- _setup_func(funcopy, mock)
- return funcopy
-
-
def _setup_func(funcopy, mock):
funcopy.mock = mock
@@ -809,8 +737,7 @@
if not _is_instance_mock(value):
setattr(type(self), name, _get_method(name, value))
original = value
- real = lambda *args, **kw: original(self, *args, **kw)
- value = mocksignature(value, real, skipfirst=True)
+ value = lambda *args, **kw: original(self, *args, **kw)
else:
# only set _new_name and not name so that mock_calls is tracked
# but not method calls
@@ -1138,7 +1065,7 @@
def __init__(
self, getter, attribute, new, spec, create,
- mocksignature, spec_set, autospec, new_callable, kwargs
+ spec_set, autospec, new_callable, kwargs
):
if new_callable is not None:
if new is not DEFAULT:
@@ -1157,7 +1084,6 @@
self.spec = spec
self.create = create
self.has_local = False
- self.mocksignature = mocksignature
self.spec_set = spec_set
self.autospec = autospec
self.kwargs = kwargs
@@ -1167,7 +1093,7 @@
def copy(self):
patcher = _patch(
self.getter, self.attribute, self.new, self.spec,
- self.create, self.mocksignature, self.spec_set,
+ self.create, self.spec_set,
self.autospec, self.new_callable, self.kwargs
)
patcher.attribute_name = self.attribute_name
@@ -1321,8 +1247,7 @@
elif autospec is not False:
# spec is ignored, new *must* be default, spec_set is treated
# as a boolean. Should we check spec is not None and that spec_set
- # is a bool? mocksignature should also not be used. Should we
- # check this?
+ # is a bool?
if new is not DEFAULT:
raise TypeError(
"autospec creates the mock for you. Can't specify "
@@ -1340,8 +1265,6 @@
raise TypeError("Can't pass kwargs to a mock we aren't creating")
new_attr = new
- if self.mocksignature:
- new_attr = mocksignature(original, new)
self.temp_original = original
self.is_local = local
@@ -1396,19 +1319,19 @@
def _patch_object(
target, attribute, new=DEFAULT, spec=None,
- create=False, mocksignature=False, spec_set=None, autospec=False,
+ create=False, spec_set=None, autospec=False,
new_callable=None, **kwargs
):
"""
patch.object(target, attribute, new=DEFAULT, spec=None, create=False,
- mocksignature=False, spec_set=None, autospec=False,
+ spec_set=None, autospec=False,
new_callable=None, **kwargs)
patch the named member (`attribute`) on an object (`target`) with a mock
object.
`patch.object` can be used as a decorator, class decorator or a context
- manager. Arguments `new`, `spec`, `create`, `mocksignature`, `spec_set`,
+ manager. Arguments `new`, `spec`, `create`, `spec_set`,
`autospec` and `new_callable` have the same meaning as for `patch`. Like
`patch`, `patch.object` takes arbitrary keyword arguments for configuring
the mock object it creates.
@@ -1418,13 +1341,13 @@
"""
getter = lambda: target
return _patch(
- getter, attribute, new, spec, create, mocksignature,
+ getter, attribute, new, spec, create,
spec_set, autospec, new_callable, kwargs
)
def _patch_multiple(target, spec=None, create=False,
- mocksignature=False, spec_set=None, autospec=False,
+ spec_set=None, autospec=False,
new_callable=None, **kwargs
):
"""Perform multiple patches in a single call. It takes the object to be
@@ -1440,7 +1363,7 @@
used as a context manager.
`patch.multiple` can be used as a decorator, class decorator or a context
- manager. The arguments `spec`, `spec_set`, `create`, `mocksignature`,
+ manager. The arguments `spec`, `spec_set`, `create`,
`autospec` and `new_callable` have the same meaning as for `patch`. These
arguments will be applied to *all* patches done by `patch.multiple`.
@@ -1460,13 +1383,13 @@
items = list(kwargs.items())
attribute, new = items[0]
patcher = _patch(
- getter, attribute, new, spec, create, mocksignature, spec_set,
+ getter, attribute, new, spec, create, spec_set,
autospec, new_callable, {}
)
patcher.attribute_name = attribute
for attribute, new in items[1:]:
this_patcher = _patch(
- getter, attribute, new, spec, create, mocksignature, spec_set,
+ getter, attribute, new, spec, create, spec_set,
autospec, new_callable, {}
)
this_patcher.attribute_name = attribute
@@ -1476,7 +1399,7 @@
def patch(
target, new=DEFAULT, spec=None, create=False,
- mocksignature=False, spec_set=None, autospec=False,
+ spec_set=None, autospec=False,
new_callable=None, **kwargs
):
"""
@@ -1507,21 +1430,14 @@
A more powerful form of `spec` is `autospec`. If you set `autospec=True`
then the mock with be created with a spec from the object being replaced.
All attributes of the mock will also have the spec of the corresponding
- attribute of the object being replaced. Methods and functions being mocked
- will have their arguments checked and will raise a `TypeError` if they are
- called with the wrong signature (similar to `mocksignature`). For mocks
- replacing a class, their return value (the 'instance') will have the same
- spec as the class.
+ attribute of the object being replaced. Methods and functions being
+ mocked will have their arguments checked and will raise a `TypeError` if
+ they are called with the wrong signature. For mocks replacing a class,
+ their return value (the 'instance') will have the same spec as the class.
Instead of `autospec=True` you can pass `autospec=some_object` to use an
arbitrary object as the spec instead of the one being replaced.
- If `mocksignature` is True then the patch will be done with a function
- created by mocking the one being replaced. If the object being replaced is
- a class then the signature of `__init__` will be copied. If the object
- being replaced is a callable object then the signature of `__call__` will
- be copied.
-
By default `patch` will fail to replace attributes that don't exist. If
you pass in `create=True`, and the attribute doesn't exist, patch will
create the attribute for you when the patched function is called, and
@@ -1550,7 +1466,7 @@
"""
getter, attribute = _get_target(target)
return _patch(
- getter, attribute, new, spec, create, mocksignature,
+ getter, attribute, new, spec, create,
spec_set, autospec, new_callable, kwargs
)
@@ -2114,9 +2030,8 @@
mock will use the corresponding attribute on the `spec` object as their
spec.
- Functions or methods being mocked will have their arguments checked in a
- similar way to `mocksignature` to check that they are called with the
- correct signature.
+ Functions or methods being mocked will have their arguments checked
+ to check that they are called with the correct signature.
If `spec_set` is True then attempting to set attributes that don't exist
on the spec object will raise an `AttributeError`.
@@ -2183,7 +2098,7 @@
continue
if isinstance(spec, FunctionTypes) and entry in FunctionAttributes:
- # allow a mock to actually be a function from mocksignature
+ # allow a mock to actually be a function
continue
# XXXX do we need a better way of getting attributes without
@@ -2214,7 +2129,7 @@
skipfirst = _must_skip(spec, entry, is_type)
_check_signature(original, new, skipfirst=skipfirst)
- # so functions created with mocksignature become instance attributes,
+ # so functions created with _set_signature become instance attributes,
# *plus* their underlying mock exists in _mock_children of the parent
# mock. Adding to _mock_children may be unnecessary where we are also
# setting as an instance attribute?
diff --git a/tests/testhelpers.py b/tests/testhelpers.py
index 8f8b6d7..1b3dd2f 100644
--- a/tests/testhelpers.py
+++ b/tests/testhelpers.py
@@ -6,8 +6,7 @@
from mock import (
call, _Call, create_autospec,
- MagicMock, Mock, ANY, _CallList,
- mocksignature
+ MagicMock, Mock, ANY, _CallList
)
from datetime import datetime
@@ -424,7 +423,7 @@
def test_builtin_functions_types(self):
- # we could replace builtin functions / methods with a mocksignature
+ # we could replace builtin functions / methods with a function
# with *args / **kwargs signature. Using the builtin method type
# as a spec seems to work fairly well though.
class BuiltinSubclass(list):
@@ -814,28 +813,28 @@
class TestCallList(unittest2.TestCase):
def test_args_list_contains_call_list(self):
- for mock in Mock(), mocksignature(lambda *args, **kwargs: None):
- self.assertIsInstance(mock.call_args_list, _CallList)
+ mock = Mock()
+ self.assertIsInstance(mock.call_args_list, _CallList)
- mock(1, 2)
- mock(a=3)
- mock(3, 4)
- mock(b=6)
+ mock(1, 2)
+ mock(a=3)
+ mock(3, 4)
+ mock(b=6)
- for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
- self.assertTrue(kall in mock.call_args_list)
+ for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
+ self.assertTrue(kall in mock.call_args_list)
- calls = [call(a=3), call(3, 4)]
- self.assertTrue(calls in mock.call_args_list)
- calls = [call(1, 2), call(a=3)]
- self.assertTrue(calls in mock.call_args_list)
- calls = [call(3, 4), call(b=6)]
- self.assertTrue(calls in mock.call_args_list)
- calls = [call(3, 4)]
- self.assertTrue(calls in mock.call_args_list)
+ calls = [call(a=3), call(3, 4)]
+ self.assertTrue(calls in mock.call_args_list)
+ calls = [call(1, 2), call(a=3)]
+ self.assertTrue(calls in mock.call_args_list)
+ calls = [call(3, 4), call(b=6)]
+ self.assertTrue(calls in mock.call_args_list)
+ calls = [call(3, 4)]
+ self.assertTrue(calls in mock.call_args_list)
- self.assertFalse(call('fish') in mock.call_args_list)
- self.assertFalse([call('fish')] in mock.call_args_list)
+ self.assertFalse(call('fish') in mock.call_args_list)
+ self.assertFalse([call('fish')] in mock.call_args_list)
def test_call_list_str(self):
diff --git a/tests/testmagicmethods.py b/tests/testmagicmethods.py
index 6dac3cc..b636341 100644
--- a/tests/testmagicmethods.py
+++ b/tests/testmagicmethods.py
@@ -51,13 +51,7 @@
mock.__getitem__ = f
self.assertFalse(mock.__getitem__ is f)
self.assertEqual(mock['foo'], (mock, 'fish'))
-
- # When you pull the function back of the *instance*
- # the first argument (self) is removed
- def instance_f(name):
- pass
- self.assertEqual(inspect.getargspec(mock.__getitem__),
- inspect.getargspec(instance_f))
+ self.assertEqual(mock.__getitem__('foo'), (mock, 'fish'))
mock.__getitem__ = mock
self.assertTrue(mock.__getitem__ is mock)
@@ -438,7 +432,8 @@
# this seems like it should work, but is hard to do without introducing
# other api inconsistencies. Failure message could be better though.
- self.assertRaises(TypeError, setattr, m, '__iter__', [3].__iter__)
+ m.__iter__ = [3].__iter__
+ self.assertRaises(TypeError, iter, m)
def test_magic_method_type(self):
diff --git a/tests/testmock.py b/tests/testmock.py
index 31ea50c..886f9bf 100644
--- a/tests/testmock.py
+++ b/tests/testmock.py
@@ -14,7 +14,7 @@
call, DEFAULT, patch, sentinel,
MagicMock, Mock, NonCallableMock,
NonCallableMagicMock, _CallList,
- mocksignature, create_autospec
+ create_autospec
)
@@ -854,7 +854,6 @@
def test_arg_lists(self):
mocks = [
Mock(),
- mocksignature(lambda *args, **kwargs: None),
MagicMock(),
NonCallableMock(),
NonCallableMagicMock()
@@ -879,10 +878,6 @@
mock.reset_mock()
assert_attrs(mock)
- if not isinstance(mock, Mock):
- # the mocksignature
- continue
-
mock.foo()
mock.foo.bar(1, a=3)
mock.foo(1).bar().baz(3)
@@ -955,48 +950,48 @@
def test_assert_has_calls_any_order(self):
- for mock in Mock(), mocksignature(lambda *args, **kwargs: None):
- mock(1, 2)
- mock(a=3)
- mock(3, 4)
- mock(b=6)
- mock(b=6)
+ mock = Mock()
+ mock(1, 2)
+ mock(a=3)
+ mock(3, 4)
+ mock(b=6)
+ mock(b=6)
- kalls = [
- call(1, 2), ({'a': 3},),
- ((3, 4),), ((), {'a': 3}),
- ('', (1, 2)), ('', {'a': 3}),
- ('', (1, 2), {}), ('', (), {'a': 3})
- ]
- for kall in kalls:
- mock.assert_has_calls([kall], any_order=True)
+ kalls = [
+ call(1, 2), ({'a': 3},),
+ ((3, 4),), ((), {'a': 3}),
+ ('', (1, 2)), ('', {'a': 3}),
+ ('', (1, 2), {}), ('', (), {'a': 3})
+ ]
+ for kall in kalls:
+ mock.assert_has_calls([kall], any_order=True)
- for kall in call(1, '2'), call(b=3), call(), 3, None, 'foo':
- self.assertRaises(
- AssertionError, mock.assert_has_calls,
- [kall], any_order=True
- )
+ for kall in call(1, '2'), call(b=3), call(), 3, None, 'foo':
+ self.assertRaises(
+ AssertionError, mock.assert_has_calls,
+ [kall], any_order=True
+ )
- kall_lists = [
- [call(1, 2), call(b=6)],
- [call(3, 4), call(1, 2)],
- [call(b=6), call(b=6)],
- ]
+ kall_lists = [
+ [call(1, 2), call(b=6)],
+ [call(3, 4), call(1, 2)],
+ [call(b=6), call(b=6)],
+ ]
- for kall_list in kall_lists:
- mock.assert_has_calls(kall_list, any_order=True)
+ for kall_list in kall_lists:
+ mock.assert_has_calls(kall_list, any_order=True)
- kall_lists = [
- [call(b=6), call(b=6), call(b=6)],
- [call(1, 2), call(1, 2)],
- [call(3, 4), call(1, 2), call(5, 7)],
- [call(b=6), call(3, 4), call(b=6), call(1, 2), call(b=6)],
- ]
- for kall_list in kall_lists:
- self.assertRaises(
- AssertionError, mock.assert_has_calls,
- kall_list, any_order=True
- )
+ kall_lists = [
+ [call(b=6), call(b=6), call(b=6)],
+ [call(1, 2), call(1, 2)],
+ [call(3, 4), call(1, 2), call(5, 7)],
+ [call(b=6), call(3, 4), call(b=6), call(1, 2), call(b=6)],
+ ]
+ for kall_list in kall_lists:
+ self.assertRaises(
+ AssertionError, mock.assert_has_calls,
+ kall_list, any_order=True
+ )
def test_assert_has_calls(self):
kalls1 = [
@@ -1009,7 +1004,7 @@
kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list())
mocks = []
- for mock in Mock(), mocksignature(lambda *args, **kwargs: None):
+ for mock in Mock(), MagicMock():
mock(1, 2)
mock(a=3)
mock(3, 4)
@@ -1039,39 +1034,39 @@
def test_assert_any_call(self):
- for mock in Mock(), mocksignature(lambda *args, **kwargs: None):
- mock(1, 2)
- mock(a=3)
- mock(1, b=6)
+ mock = Mock()
+ mock(1, 2)
+ mock(a=3)
+ mock(1, b=6)
- mock.assert_any_call(1, 2)
- mock.assert_any_call(a=3)
- mock.assert_any_call(1, b=6)
+ mock.assert_any_call(1, 2)
+ mock.assert_any_call(a=3)
+ mock.assert_any_call(1, b=6)
- self.assertRaises(
- AssertionError,
- mock.assert_any_call
- )
- self.assertRaises(
- AssertionError,
- mock.assert_any_call,
- 1, 3
- )
- self.assertRaises(
- AssertionError,
- mock.assert_any_call,
- a=4
- )
+ self.assertRaises(
+ AssertionError,
+ mock.assert_any_call
+ )
+ self.assertRaises(
+ AssertionError,
+ mock.assert_any_call,
+ 1, 3
+ )
+ self.assertRaises(
+ AssertionError,
+ mock.assert_any_call,
+ a=4
+ )
- def test_mock_calls_mocksignature(self):
+ def test_mock_calls_create_autospec(self):
def f(a, b):
pass
obj = Iter()
obj.f = f
funcs = [
- mocksignature(f), create_autospec(f),
+ create_autospec(f),
create_autospec(obj).f
]
for func in funcs:
diff --git a/tests/testmocksignature.py b/tests/testmocksignature.py
deleted file mode 100644
index 7bdc536..0000000
--- a/tests/testmocksignature.py
+++ /dev/null
@@ -1,345 +0,0 @@
-# Copyright (C) 2007-2012 Michael Foord & the mock team
-# E-mail: fuzzyman AT voidspace DOT org DOT uk
-# http://www.voidspace.org.uk/python/mock/
-
-import inspect
-
-from tests.support import unittest2
-
-from mock import Mock, mocksignature, patch
-
-
-class Something(object):
- def __init__(self, foo, bar=10):
- pass
- def __call__(self, foo, bar=5):
- pass
-
-something = Something(1, 2)
-
-
-def f(a, b, c):
- pass
-
-
-class TestMockSignature(unittest2.TestCase):
-
- def test_function(self):
- def f(a):
- pass
- mock = Mock()
-
- f2 = mocksignature(f, mock)
- self.assertIs(f2.mock, mock)
-
- self.assertRaises(TypeError, f2)
- mock.return_value = 3
- self.assertEqual(f2('foo'), 3)
- mock.assert_called_with('foo')
- f2.mock.assert_called_with('foo')
-
-
- def test_function_without_explicit_mock(self):
- def f(a):
- pass
-
- f2 = mocksignature(f)
- self.assertIsInstance(f2.mock, Mock)
-
- self.assertRaises(TypeError, f2)
- f2.mock.return_value = 3
- self.assertEqual(f2('foo'), 3)
- f2.mock.assert_called_with('foo')
-
-
- def test_method(self):
- class Foo(object):
- def method(self, a, b):
- pass
-
- f = Foo()
- mock = Mock()
- mock.return_value = 3
- f.method = mocksignature(f.method, mock)
- self.assertEqual(f.method('foo', 'bar'), 3)
- mock.assert_called_with('foo', 'bar')
-
-
- def test_function_with_defaults(self):
- def f(a, b=None):
- pass
- mock = Mock()
- f2 = mocksignature(f, mock)
- f2(3)
- mock.assert_called_with(3, None)
- mock.reset_mock()
-
- f2(1, 7)
- mock.assert_called_with(1, 7)
- mock.reset_mock()
-
- f2(b=1, a=7)
- mock.assert_called_with(7, 1)
- mock.reset_mock()
-
- a = object()
- def f(a=a):
- pass
- f2 = mocksignature(f, mock)
- f2()
- mock.assert_called_with(a)
-
-
- def test_introspection(self):
- def f(a, *args, **kwargs):
- pass
- f2 = mocksignature(f, f)
- self.assertEqual(inspect.getargspec(f), inspect.getargspec(f2))
-
- def f(a, b=None, c=3, d=object()):
- pass
- f2 = mocksignature(f, f)
- self.assertEqual(inspect.getargspec(f), inspect.getargspec(f2))
-
-
- def test_function_with_varargs_and_kwargs(self):
- def f(a, b=None, *args, **kwargs):
- return (a, b, args, kwargs)
- f2 = mocksignature(f, f)
- self.assertEqual(f2(3, 4, 5, x=6, y=9), (3, 4, (5,), {'x': 6, 'y': 9}))
- self.assertEqual(f2(3, x=6, y=9, b='a'), (3, 'a', (), {'x': 6, 'y': 9}))
-
- def f(*args):
- pass
- g = mocksignature(f)
- g.mock.return_value = 3
- self.assertEqual(g(1, 2, 'many'), 3)
- self.assertEqual(g(), 3)
- self.assertRaises(TypeError, lambda: g(a=None))
-
- def f(**kwargs):
- pass
- g = mocksignature(f)
- g.mock.return_value = 3
- self.assertEqual(g(), 3)
- self.assertEqual(g(a=None, b=None), 3)
- self.assertRaises(TypeError, lambda: g(None))
-
-
- def test_mocksignature_with_patch(self):
- mock = Mock()
-
- def f(a, b, c):
- pass
- mock.f = f
-
- @patch.object(mock, 'f', mocksignature=True)
- def test(mock_f):
- self.assertRaises(TypeError, mock.f, 3, 4)
- self.assertRaises(TypeError, mock.f, 3, 4, 5, 6)
- mock.f(1, 2, 3)
-
- mock_f.assert_called_with(1, 2, 3)
- mock.f.mock.assert_called_with(1, 2, 3)
-
- test()
-
- @patch('tests.support.SomeClass.wibble', mocksignature=True)
- def test(mock_wibble):
- from tests.support import SomeClass
-
- instance = SomeClass()
- self.assertRaises(TypeError, instance.wibble, 1)
- instance.wibble()
-
- mock_wibble.assert_called_with(instance)
- instance.wibble.mock.assert_called_with(instance)
-
- test()
-
-
- @unittest2.skipUnless(__debug__, 'assert disabled when run with -O/OO')
- def test_mocksignature_with_reserved_arg(self):
- def f(_mock_):
- pass
- self.assertRaises(AssertionError, lambda: mocksignature(f))
-
- def f(_mock_=None):
- pass
- self.assertRaises(AssertionError, lambda: mocksignature(f))
-
- def f(*_mock_):
- pass
- self.assertRaises(AssertionError, lambda: mocksignature(f))
-
- def f(**_mock_):
- pass
- self.assertRaises(AssertionError, lambda: mocksignature(f))
-
-
- def test_mocksignature_class(self):
- MockedSomething = mocksignature(Something)
-
- result = MockedSomething(5, 23)
- self.assertIs(result, MockedSomething.mock.return_value)
-
- MockedSomething(1)
- MockedSomething.mock.assert_caled_with(1, 10)
-
- self.assertRaises(TypeError, MockedSomething)
-
-
- def test_mocksignature_callable(self):
- mocked_something = mocksignature(something)
-
- result = mocked_something(5, 23)
- self.assertIs(result, mocked_something.mock.return_value)
-
- mocked_something(1)
- mocked_something.mock.assert_caled_with(1, 5)
-
- self.assertRaises(TypeError, mocked_something)
-
-
- def test_patch_mocksignature_class(self):
- original_something = Something
- something_name = '%s.Something' % __name__
- @patch(something_name, mocksignature=True)
- def test(MockSomething):
- Something(3, 5)
- MockSomething.assert_called_with(3, 5)
-
- Something(6)
- MockSomething.assert_called_with(6, 10)
-
- self.assertRaises(TypeError, Something)
- test()
- self.assertIs(Something, original_something)
-
-
- def test_patch_mocksignature_callable(self):
- original_something = something
- something_name = '%s.something' % __name__
- @patch(something_name, mocksignature=True)
- def test(MockSomething):
- something(3, 4)
- MockSomething.assert_called_with(3, 4)
-
- something(6)
- MockSomething.assert_called_with(6, 5)
-
- self.assertRaises(TypeError, something)
- test()
- self.assertIs(something, original_something)
-
-
- def test_patchobject_mocksignature(self):
- class something(object):
- def meth(self, a, b, c):
- pass
-
- original = something.__dict__['meth']
-
- @patch.object(something, 'meth', mocksignature=True)
- def test(_):
- self.assertIsNot(something.__dict__['meth'], original)
- thing = something()
- thing.meth(1, 2, 3)
- self.assertRaises(TypeError, thing.meth, 1)
-
- test()
- self.assertIs(something.__dict__['meth'], original)
-
- thing = something()
-
- original = thing.meth
- @patch.object(thing, 'meth', mocksignature=True)
- def test(_):
- thing.meth(1, 2, 3)
- self.assertRaises(TypeError, thing.meth, 1)
-
- test()
- self.assertEqual(thing.meth, original)
-
- # when patching instance methods using mocksignatures we used to
- # replace the bound method with an instance attribute on unpatching.
- self.assertNotIn('meth', thing.__dict__)
-
-
- def test_assert_called_with(self):
- func = mocksignature(f)
-
- self.assertRaises(AssertionError, func.assert_called_with)
- self.assertRaises(AssertionError, func.assert_called_once_with)
-
- func(1, 2, 3)
- func.assert_called_with(1, 2, 3)
- self.assertRaises(AssertionError, func.assert_called_with, 4, 5, 6)
- func.assert_called_once_with(1, 2, 3)
- self.assertRaises(AssertionError, func.assert_called_once_with,
- 4, 5, 6)
-
-
- def test_mock_attributes(self):
- func = mocksignature(f)
-
- return_value = func.return_value
- self.assertIsInstance(return_value, Mock)
- self.assertIsNone(func.side_effect)
- self.assertFalse(func.called)
- self.assertIsNone(func.call_args)
- self.assertEqual(func.call_count, 0)
- self.assertEqual(func.method_calls, [])
- self.assertEqual(func.call_args_list, [])
- self.assertIs(func._mock_children, func.mock._mock_children)
-
- self.assertIs(func(1, 2, 3), return_value)
-
- self.assertTrue(func.called)
- self.assertEqual(func.call_args, ((1, 2, 3), {}))
- self.assertEqual(func.call_count, 1)
- self.assertEqual(func.method_calls, [])
- self.assertEqual(func.call_args_list, [((1, 2, 3), {})])
- func.method_calls.append('foo')
-
- return_value()
- func.reset_mock()
-
- self.assertEqual(return_value.call_count, False)
- self.assertFalse(func.called)
- self.assertIsNone(func.call_args)
- self.assertEqual(func.call_count, 0)
- self.assertEqual(func.method_calls, [])
- self.assertEqual(func.call_args_list, [])
- self.assertIs(func._mock_children, func.mock._mock_children)
-
- func.side_effect = KeyError
- self.assertRaises(KeyError, func, 1, 2, 3)
- self.assertTrue(func.called)
-
- func.side_effect = None
- func.return_value = 'foo'
- self.assertEqual(func(1, 2, 3), 'foo')
- self.assertEqual(func.call_count, 2)
-
-
- def test_return_value_from_existing_mock(self):
- mock = Mock(return_value='foo')
- func = mocksignature(f, mock)
- self.assertEqual(func(1, 2, 3), 'foo')
-
- mock.return_value = 'bar'
- self.assertEqual(func(1, 2, 3), 'bar')
-
-
- def test_side_effect_from_existing_mock(self):
- mock = Mock(side_effect=KeyError)
- func = mocksignature(f, mock)
- self.assertRaises(KeyError, func, 1, 2, 3)
-
- mock.side_effect = NameError
- self.assertRaises(NameError, func, 1, 2, 3)
-
-
-if __name__ == '__main__':
- unittest2.main()
diff --git a/tests/testpatch.py b/tests/testpatch.py
index 3c4f823..5c08bfc 100644
--- a/tests/testpatch.py
+++ b/tests/testpatch.py
@@ -610,7 +610,7 @@
foo = {}
@patch('%s.SomeClass' % __name__, object())
- @patch('%s.SomeClass' % __name__, object(), mocksignature=True)
+ @patch('%s.SomeClass' % __name__, object(), autospec=True)
@patch.object(SomeClass, object())
@patch.dict(foo)
def some_name():