Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Port tests from original PR
  • Loading branch information
voidspace committed Sep 13, 2019
commit 890795e5f06f3683f351c090ec376ea6ae6b4123
49 changes: 49 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,48 @@ def test_only_allowed_methods_exist(self):
)


def _check_autospeced_something(self, something):
for method_name in ['meth', 'cmeth', 'smeth']:
mock_method = getattr(something, method_name)

# check that the methods are callable with correct args.
mock_method(sentinel.a, sentinel.b, sentinel.c)
mock_method(sentinel.a, sentinel.b, sentinel.c, d=sentinel.d)
mock_method.assert_has_calls([
call(sentinel.a, sentinel.b, sentinel.c),
call(sentinel.a, sentinel.b, sentinel.c, d=sentinel.d)])

# assert that TypeError is raised if the method signature is not
# respected.
self.assertRaises(TypeError, mock_method)
self.assertRaises(TypeError, mock_method, sentinel.a)
self.assertRaises(TypeError, mock_method, a=sentinel.a)
self.assertRaises(TypeError, mock_method, sentinel.a, sentinel.b,
sentinel.c, e=sentinel.e)

# assert that AttributeError is raised if the method does not exist.
self.assertRaises(AttributeError, getattr, something, 'foolish')


def test_mock_autospec_all_members(self):
for spec in [Something, Something()]:
mock_something = Mock(autospec=spec)
self._check_autospeced_something(mock_something)


def test_mock_spec_function(self):
def foo(lish):
pass

mock_foo = Mock(spec=foo)

mock_foo(sentinel.lish)
mock_foo.assert_called_once_with(sentinel.lish)
self.assertRaises(TypeError, mock_foo)
self.assertRaises(TypeError, mock_foo, sentinel.foo, sentinel.lish)
self.assertRaises(TypeError, mock_foo, foo=sentinel.foo)


def test_from_spec(self):
class Something(object):
x = 3
Expand Down Expand Up @@ -1387,6 +1429,13 @@ def meth1(self, a, b): pass
)


def test_mock_add_spec_autospec_all_members(self):
for spec in [Something, Something()]:
mock_something = Mock()
mock_something.mock_add_spec(spec, autospec=True)
self._check_autospeced_something(mock_something)


def test_assert_has_calls_nested_without_spec(self):
m = MagicMock()
m().foo().bar().baz()
Expand Down