michael@0: # Copyright (C) 2007-2012 Michael Foord & the mock team michael@0: # E-mail: fuzzyman AT voidspace DOT org DOT uk michael@0: # http://www.voidspace.org.uk/python/mock/ michael@0: michael@0: from tests.support import unittest2, inPy3k michael@0: michael@0: from mock import ( michael@0: call, _Call, create_autospec, MagicMock, michael@0: Mock, ANY, _CallList, patch, PropertyMock michael@0: ) michael@0: michael@0: from datetime import datetime michael@0: michael@0: class SomeClass(object): michael@0: def one(self, a, b): michael@0: pass michael@0: def two(self): michael@0: pass michael@0: def three(self, a=None): michael@0: pass michael@0: michael@0: michael@0: michael@0: class AnyTest(unittest2.TestCase): michael@0: michael@0: def test_any(self): michael@0: self.assertEqual(ANY, object()) michael@0: michael@0: mock = Mock() michael@0: mock(ANY) michael@0: mock.assert_called_with(ANY) michael@0: michael@0: mock = Mock() michael@0: mock(foo=ANY) michael@0: mock.assert_called_with(foo=ANY) michael@0: michael@0: def test_repr(self): michael@0: self.assertEqual(repr(ANY), '') michael@0: self.assertEqual(str(ANY), '') michael@0: michael@0: michael@0: def test_any_and_datetime(self): michael@0: mock = Mock() michael@0: mock(datetime.now(), foo=datetime.now()) michael@0: michael@0: mock.assert_called_with(ANY, foo=ANY) michael@0: michael@0: michael@0: def test_any_mock_calls_comparison_order(self): michael@0: mock = Mock() michael@0: d = datetime.now() michael@0: class Foo(object): michael@0: def __eq__(self, other): michael@0: return False michael@0: def __ne__(self, other): michael@0: return True michael@0: michael@0: for d in datetime.now(), Foo(): michael@0: mock.reset_mock() michael@0: michael@0: mock(d, foo=d, bar=d) michael@0: mock.method(d, zinga=d, alpha=d) michael@0: mock().method(a1=d, z99=d) michael@0: michael@0: expected = [ michael@0: call(ANY, foo=ANY, bar=ANY), michael@0: call.method(ANY, zinga=ANY, alpha=ANY), michael@0: call(), call().method(a1=ANY, z99=ANY) michael@0: ] michael@0: self.assertEqual(expected, mock.mock_calls) michael@0: self.assertEqual(mock.mock_calls, expected) michael@0: michael@0: michael@0: michael@0: class CallTest(unittest2.TestCase): michael@0: michael@0: def test_call_with_call(self): michael@0: kall = _Call() michael@0: self.assertEqual(kall, _Call()) michael@0: self.assertEqual(kall, _Call(('',))) michael@0: self.assertEqual(kall, _Call(((),))) michael@0: self.assertEqual(kall, _Call(({},))) michael@0: self.assertEqual(kall, _Call(('', ()))) michael@0: self.assertEqual(kall, _Call(('', {}))) michael@0: self.assertEqual(kall, _Call(('', (), {}))) michael@0: self.assertEqual(kall, _Call(('foo',))) michael@0: self.assertEqual(kall, _Call(('bar', ()))) michael@0: self.assertEqual(kall, _Call(('baz', {}))) michael@0: self.assertEqual(kall, _Call(('spam', (), {}))) michael@0: michael@0: kall = _Call(((1, 2, 3),)) michael@0: self.assertEqual(kall, _Call(((1, 2, 3),))) michael@0: self.assertEqual(kall, _Call(('', (1, 2, 3)))) michael@0: self.assertEqual(kall, _Call(((1, 2, 3), {}))) michael@0: self.assertEqual(kall, _Call(('', (1, 2, 3), {}))) michael@0: michael@0: kall = _Call(((1, 2, 4),)) michael@0: self.assertNotEqual(kall, _Call(('', (1, 2, 3)))) michael@0: self.assertNotEqual(kall, _Call(('', (1, 2, 3), {}))) michael@0: michael@0: kall = _Call(('foo', (1, 2, 4),)) michael@0: self.assertNotEqual(kall, _Call(('', (1, 2, 4)))) michael@0: self.assertNotEqual(kall, _Call(('', (1, 2, 4), {}))) michael@0: self.assertNotEqual(kall, _Call(('bar', (1, 2, 4)))) michael@0: self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {}))) michael@0: michael@0: kall = _Call(({'a': 3},)) michael@0: self.assertEqual(kall, _Call(('', (), {'a': 3}))) michael@0: self.assertEqual(kall, _Call(('', {'a': 3}))) michael@0: self.assertEqual(kall, _Call(((), {'a': 3}))) michael@0: self.assertEqual(kall, _Call(({'a': 3},))) michael@0: michael@0: michael@0: def test_empty__Call(self): michael@0: args = _Call() michael@0: michael@0: self.assertEqual(args, ()) michael@0: self.assertEqual(args, ('foo',)) michael@0: self.assertEqual(args, ((),)) michael@0: self.assertEqual(args, ('foo', ())) michael@0: self.assertEqual(args, ('foo',(), {})) michael@0: self.assertEqual(args, ('foo', {})) michael@0: self.assertEqual(args, ({},)) michael@0: michael@0: michael@0: def test_named_empty_call(self): michael@0: args = _Call(('foo', (), {})) michael@0: michael@0: self.assertEqual(args, ('foo',)) michael@0: self.assertEqual(args, ('foo', ())) michael@0: self.assertEqual(args, ('foo',(), {})) michael@0: self.assertEqual(args, ('foo', {})) michael@0: michael@0: self.assertNotEqual(args, ((),)) michael@0: self.assertNotEqual(args, ()) michael@0: self.assertNotEqual(args, ({},)) michael@0: self.assertNotEqual(args, ('bar',)) michael@0: self.assertNotEqual(args, ('bar', ())) michael@0: self.assertNotEqual(args, ('bar', {})) michael@0: michael@0: michael@0: def test_call_with_args(self): michael@0: args = _Call(((1, 2, 3), {})) michael@0: michael@0: self.assertEqual(args, ((1, 2, 3),)) michael@0: self.assertEqual(args, ('foo', (1, 2, 3))) michael@0: self.assertEqual(args, ('foo', (1, 2, 3), {})) michael@0: self.assertEqual(args, ((1, 2, 3), {})) michael@0: michael@0: michael@0: def test_named_call_with_args(self): michael@0: args = _Call(('foo', (1, 2, 3), {})) michael@0: michael@0: self.assertEqual(args, ('foo', (1, 2, 3))) michael@0: self.assertEqual(args, ('foo', (1, 2, 3), {})) michael@0: michael@0: self.assertNotEqual(args, ((1, 2, 3),)) michael@0: self.assertNotEqual(args, ((1, 2, 3), {})) michael@0: michael@0: michael@0: def test_call_with_kwargs(self): michael@0: args = _Call(((), dict(a=3, b=4))) michael@0: michael@0: self.assertEqual(args, (dict(a=3, b=4),)) michael@0: self.assertEqual(args, ('foo', dict(a=3, b=4))) michael@0: self.assertEqual(args, ('foo', (), dict(a=3, b=4))) michael@0: self.assertEqual(args, ((), dict(a=3, b=4))) michael@0: michael@0: michael@0: def test_named_call_with_kwargs(self): michael@0: args = _Call(('foo', (), dict(a=3, b=4))) michael@0: michael@0: self.assertEqual(args, ('foo', dict(a=3, b=4))) michael@0: self.assertEqual(args, ('foo', (), dict(a=3, b=4))) michael@0: michael@0: self.assertNotEqual(args, (dict(a=3, b=4),)) michael@0: self.assertNotEqual(args, ((), dict(a=3, b=4))) michael@0: michael@0: michael@0: def test_call_with_args_call_empty_name(self): michael@0: args = _Call(((1, 2, 3), {})) michael@0: self.assertEqual(args, call(1, 2, 3)) michael@0: self.assertEqual(call(1, 2, 3), args) michael@0: self.assertTrue(call(1, 2, 3) in [args]) michael@0: michael@0: michael@0: def test_call_ne(self): michael@0: self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2)) michael@0: self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3)) michael@0: self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3)) michael@0: michael@0: michael@0: def test_call_non_tuples(self): michael@0: kall = _Call(((1, 2, 3),)) michael@0: for value in 1, None, self, int: michael@0: self.assertNotEqual(kall, value) michael@0: self.assertFalse(kall == value) michael@0: michael@0: michael@0: def test_repr(self): michael@0: self.assertEqual(repr(_Call()), 'call()') michael@0: self.assertEqual(repr(_Call(('foo',))), 'call.foo()') michael@0: michael@0: self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))), michael@0: "call(1, 2, 3, a='b')") michael@0: self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))), michael@0: "call.bar(1, 2, 3, a='b')") michael@0: michael@0: self.assertEqual(repr(call), 'call') michael@0: self.assertEqual(str(call), 'call') michael@0: michael@0: self.assertEqual(repr(call()), 'call()') michael@0: self.assertEqual(repr(call(1)), 'call(1)') michael@0: self.assertEqual(repr(call(zz='thing')), "call(zz='thing')") michael@0: michael@0: self.assertEqual(repr(call().foo), 'call().foo') michael@0: self.assertEqual(repr(call(1).foo.bar(a=3).bing), michael@0: 'call().foo.bar().bing') michael@0: self.assertEqual( michael@0: repr(call().foo(1, 2, a=3)), michael@0: "call().foo(1, 2, a=3)" michael@0: ) michael@0: self.assertEqual(repr(call()()), "call()()") michael@0: self.assertEqual(repr(call(1)(2)), "call()(2)") michael@0: self.assertEqual( michael@0: repr(call()().bar().baz.beep(1)), michael@0: "call()().bar().baz.beep(1)" michael@0: ) michael@0: michael@0: michael@0: def test_call(self): michael@0: self.assertEqual(call(), ('', (), {})) michael@0: self.assertEqual(call('foo', 'bar', one=3, two=4), michael@0: ('', ('foo', 'bar'), {'one': 3, 'two': 4})) michael@0: michael@0: mock = Mock() michael@0: mock(1, 2, 3) michael@0: mock(a=3, b=6) michael@0: self.assertEqual(mock.call_args_list, michael@0: [call(1, 2, 3), call(a=3, b=6)]) michael@0: michael@0: def test_attribute_call(self): michael@0: self.assertEqual(call.foo(1), ('foo', (1,), {})) michael@0: self.assertEqual(call.bar.baz(fish='eggs'), michael@0: ('bar.baz', (), {'fish': 'eggs'})) michael@0: michael@0: mock = Mock() michael@0: mock.foo(1, 2 ,3) michael@0: mock.bar.baz(a=3, b=6) michael@0: self.assertEqual(mock.method_calls, michael@0: [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)]) michael@0: michael@0: michael@0: def test_extended_call(self): michael@0: result = call(1).foo(2).bar(3, a=4) michael@0: self.assertEqual(result, ('().foo().bar', (3,), dict(a=4))) michael@0: michael@0: mock = MagicMock() michael@0: mock(1, 2, a=3, b=4) michael@0: self.assertEqual(mock.call_args, call(1, 2, a=3, b=4)) michael@0: self.assertNotEqual(mock.call_args, call(1, 2, 3)) michael@0: michael@0: self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)]) michael@0: self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)]) michael@0: michael@0: mock = MagicMock() michael@0: mock.foo(1).bar()().baz.beep(a=6) michael@0: michael@0: last_call = call.foo(1).bar()().baz.beep(a=6) michael@0: self.assertEqual(mock.mock_calls[-1], last_call) michael@0: self.assertEqual(mock.mock_calls, last_call.call_list()) michael@0: michael@0: michael@0: def test_call_list(self): michael@0: mock = MagicMock() michael@0: mock(1) michael@0: self.assertEqual(call(1).call_list(), mock.mock_calls) michael@0: michael@0: mock = MagicMock() michael@0: mock(1).method(2) michael@0: self.assertEqual(call(1).method(2).call_list(), michael@0: mock.mock_calls) michael@0: michael@0: mock = MagicMock() michael@0: mock(1).method(2)(3) michael@0: self.assertEqual(call(1).method(2)(3).call_list(), michael@0: mock.mock_calls) michael@0: michael@0: mock = MagicMock() michael@0: int(mock(1).method(2)(3).foo.bar.baz(4)(5)) michael@0: kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__() michael@0: self.assertEqual(kall.call_list(), mock.mock_calls) michael@0: michael@0: michael@0: def test_call_any(self): michael@0: self.assertEqual(call, ANY) michael@0: michael@0: m = MagicMock() michael@0: int(m) michael@0: self.assertEqual(m.mock_calls, [ANY]) michael@0: self.assertEqual([ANY], m.mock_calls) michael@0: michael@0: michael@0: def test_two_args_call(self): michael@0: args = _Call(((1, 2), {'a': 3}), two=True) michael@0: self.assertEqual(len(args), 2) michael@0: self.assertEqual(args[0], (1, 2)) michael@0: self.assertEqual(args[1], {'a': 3}) michael@0: michael@0: other_args = _Call(((1, 2), {'a': 3})) michael@0: self.assertEqual(args, other_args) michael@0: michael@0: michael@0: class SpecSignatureTest(unittest2.TestCase): michael@0: michael@0: def _check_someclass_mock(self, mock): michael@0: self.assertRaises(AttributeError, getattr, mock, 'foo') michael@0: mock.one(1, 2) michael@0: mock.one.assert_called_with(1, 2) michael@0: self.assertRaises(AssertionError, michael@0: mock.one.assert_called_with, 3, 4) michael@0: self.assertRaises(TypeError, mock.one, 1) michael@0: michael@0: mock.two() michael@0: mock.two.assert_called_with() michael@0: self.assertRaises(AssertionError, michael@0: mock.two.assert_called_with, 3) michael@0: self.assertRaises(TypeError, mock.two, 1) michael@0: michael@0: mock.three() michael@0: mock.three.assert_called_with() michael@0: self.assertRaises(AssertionError, michael@0: mock.three.assert_called_with, 3) michael@0: self.assertRaises(TypeError, mock.three, 3, 2) michael@0: michael@0: mock.three(1) michael@0: mock.three.assert_called_with(1) michael@0: michael@0: mock.three(a=1) michael@0: mock.three.assert_called_with(a=1) michael@0: michael@0: michael@0: def test_basic(self): michael@0: for spec in (SomeClass, SomeClass()): michael@0: mock = create_autospec(spec) michael@0: self._check_someclass_mock(mock) michael@0: michael@0: michael@0: def test_create_autospec_return_value(self): michael@0: def f(): michael@0: pass michael@0: mock = create_autospec(f, return_value='foo') michael@0: self.assertEqual(mock(), 'foo') michael@0: michael@0: class Foo(object): michael@0: pass michael@0: michael@0: mock = create_autospec(Foo, return_value='foo') michael@0: self.assertEqual(mock(), 'foo') michael@0: michael@0: michael@0: def test_autospec_reset_mock(self): michael@0: m = create_autospec(int) michael@0: int(m) michael@0: m.reset_mock() michael@0: self.assertEqual(m.__int__.call_count, 0) michael@0: michael@0: michael@0: def test_mocking_unbound_methods(self): michael@0: class Foo(object): michael@0: def foo(self, foo): michael@0: pass michael@0: p = patch.object(Foo, 'foo') michael@0: mock_foo = p.start() michael@0: Foo().foo(1) michael@0: michael@0: mock_foo.assert_called_with(1) michael@0: michael@0: michael@0: @unittest2.expectedFailure michael@0: def test_create_autospec_unbound_methods(self): michael@0: # see issue 128 michael@0: class Foo(object): michael@0: def foo(self): michael@0: pass michael@0: michael@0: klass = create_autospec(Foo) michael@0: instance = klass() michael@0: self.assertRaises(TypeError, instance.foo, 1) michael@0: michael@0: # Note: no type checking on the "self" parameter michael@0: klass.foo(1) michael@0: klass.foo.assert_called_with(1) michael@0: self.assertRaises(TypeError, klass.foo) michael@0: michael@0: michael@0: def test_create_autospec_keyword_arguments(self): michael@0: class Foo(object): michael@0: a = 3 michael@0: m = create_autospec(Foo, a='3') michael@0: self.assertEqual(m.a, '3') michael@0: michael@0: @unittest2.skipUnless(inPy3k, "Keyword only arguments Python 3 specific") michael@0: def test_create_autospec_keyword_only_arguments(self): michael@0: func_def = "def foo(a, *, b=None):\n pass\n" michael@0: namespace = {} michael@0: exec (func_def, namespace) michael@0: foo = namespace['foo'] michael@0: michael@0: m = create_autospec(foo) michael@0: m(1) michael@0: m.assert_called_with(1) michael@0: self.assertRaises(TypeError, m, 1, 2) michael@0: michael@0: m(2, b=3) michael@0: m.assert_called_with(2, b=3) michael@0: michael@0: def test_function_as_instance_attribute(self): michael@0: obj = SomeClass() michael@0: def f(a): michael@0: pass michael@0: obj.f = f michael@0: michael@0: mock = create_autospec(obj) michael@0: mock.f('bing') michael@0: mock.f.assert_called_with('bing') michael@0: michael@0: michael@0: def test_spec_as_list(self): michael@0: # because spec as a list of strings in the mock constructor means michael@0: # something very different we treat a list instance as the type. michael@0: mock = create_autospec([]) michael@0: mock.append('foo') michael@0: mock.append.assert_called_with('foo') michael@0: michael@0: self.assertRaises(AttributeError, getattr, mock, 'foo') michael@0: michael@0: class Foo(object): michael@0: foo = [] michael@0: michael@0: mock = create_autospec(Foo) michael@0: mock.foo.append(3) michael@0: mock.foo.append.assert_called_with(3) michael@0: self.assertRaises(AttributeError, getattr, mock.foo, 'foo') michael@0: michael@0: michael@0: def test_attributes(self): michael@0: class Sub(SomeClass): michael@0: attr = SomeClass() michael@0: michael@0: sub_mock = create_autospec(Sub) michael@0: michael@0: for mock in (sub_mock, sub_mock.attr): michael@0: self._check_someclass_mock(mock) michael@0: michael@0: michael@0: def test_builtin_functions_types(self): michael@0: # we could replace builtin functions / methods with a function michael@0: # with *args / **kwargs signature. Using the builtin method type michael@0: # as a spec seems to work fairly well though. michael@0: class BuiltinSubclass(list): michael@0: def bar(self, arg): michael@0: pass michael@0: sorted = sorted michael@0: attr = {} michael@0: michael@0: mock = create_autospec(BuiltinSubclass) michael@0: mock.append(3) michael@0: mock.append.assert_called_with(3) michael@0: self.assertRaises(AttributeError, getattr, mock.append, 'foo') michael@0: michael@0: mock.bar('foo') michael@0: mock.bar.assert_called_with('foo') michael@0: self.assertRaises(TypeError, mock.bar, 'foo', 'bar') michael@0: self.assertRaises(AttributeError, getattr, mock.bar, 'foo') michael@0: michael@0: mock.sorted([1, 2]) michael@0: mock.sorted.assert_called_with([1, 2]) michael@0: self.assertRaises(AttributeError, getattr, mock.sorted, 'foo') michael@0: michael@0: mock.attr.pop(3) michael@0: mock.attr.pop.assert_called_with(3) michael@0: self.assertRaises(AttributeError, getattr, mock.attr, 'foo') michael@0: michael@0: michael@0: def test_method_calls(self): michael@0: class Sub(SomeClass): michael@0: attr = SomeClass() michael@0: michael@0: mock = create_autospec(Sub) michael@0: mock.one(1, 2) michael@0: mock.two() michael@0: mock.three(3) michael@0: michael@0: expected = [call.one(1, 2), call.two(), call.three(3)] michael@0: self.assertEqual(mock.method_calls, expected) michael@0: michael@0: mock.attr.one(1, 2) michael@0: mock.attr.two() michael@0: mock.attr.three(3) michael@0: michael@0: expected.extend( michael@0: [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)] michael@0: ) michael@0: self.assertEqual(mock.method_calls, expected) michael@0: michael@0: michael@0: def test_magic_methods(self): michael@0: class BuiltinSubclass(list): michael@0: attr = {} michael@0: michael@0: mock = create_autospec(BuiltinSubclass) michael@0: self.assertEqual(list(mock), []) michael@0: self.assertRaises(TypeError, int, mock) michael@0: self.assertRaises(TypeError, int, mock.attr) michael@0: self.assertEqual(list(mock), []) michael@0: michael@0: self.assertIsInstance(mock['foo'], MagicMock) michael@0: self.assertIsInstance(mock.attr['foo'], MagicMock) michael@0: michael@0: michael@0: def test_spec_set(self): michael@0: class Sub(SomeClass): michael@0: attr = SomeClass() michael@0: michael@0: for spec in (Sub, Sub()): michael@0: mock = create_autospec(spec, spec_set=True) michael@0: self._check_someclass_mock(mock) michael@0: michael@0: self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar') michael@0: self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar') michael@0: michael@0: michael@0: def test_descriptors(self): michael@0: class Foo(object): michael@0: @classmethod michael@0: def f(cls, a, b): michael@0: pass michael@0: @staticmethod michael@0: def g(a, b): michael@0: pass michael@0: michael@0: class Bar(Foo): michael@0: pass michael@0: michael@0: class Baz(SomeClass, Bar): michael@0: pass michael@0: michael@0: for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()): michael@0: mock = create_autospec(spec) michael@0: mock.f(1, 2) michael@0: mock.f.assert_called_once_with(1, 2) michael@0: michael@0: mock.g(3, 4) michael@0: mock.g.assert_called_once_with(3, 4) michael@0: michael@0: michael@0: @unittest2.skipIf(inPy3k, "No old style classes in Python 3") michael@0: def test_old_style_classes(self): michael@0: class Foo: michael@0: def f(self, a, b): michael@0: pass michael@0: michael@0: class Bar(Foo): michael@0: g = Foo() michael@0: michael@0: for spec in (Foo, Foo(), Bar, Bar()): michael@0: mock = create_autospec(spec) michael@0: mock.f(1, 2) michael@0: mock.f.assert_called_once_with(1, 2) michael@0: michael@0: self.assertRaises(AttributeError, getattr, mock, 'foo') michael@0: self.assertRaises(AttributeError, getattr, mock.f, 'foo') michael@0: michael@0: mock.g.f(1, 2) michael@0: mock.g.f.assert_called_once_with(1, 2) michael@0: self.assertRaises(AttributeError, getattr, mock.g, 'foo') michael@0: michael@0: michael@0: def test_recursive(self): michael@0: class A(object): michael@0: def a(self): michael@0: pass michael@0: foo = 'foo bar baz' michael@0: bar = foo michael@0: michael@0: A.B = A michael@0: mock = create_autospec(A) michael@0: michael@0: mock() michael@0: self.assertFalse(mock.B.called) michael@0: michael@0: mock.a() michael@0: mock.B.a() michael@0: self.assertEqual(mock.method_calls, [call.a(), call.B.a()]) michael@0: michael@0: self.assertIs(A.foo, A.bar) michael@0: self.assertIsNot(mock.foo, mock.bar) michael@0: mock.foo.lower() michael@0: self.assertRaises(AssertionError, mock.bar.lower.assert_called_with) michael@0: michael@0: michael@0: def test_spec_inheritance_for_classes(self): michael@0: class Foo(object): michael@0: def a(self): michael@0: pass michael@0: class Bar(object): michael@0: def f(self): michael@0: pass michael@0: michael@0: class_mock = create_autospec(Foo) michael@0: michael@0: self.assertIsNot(class_mock, class_mock()) michael@0: michael@0: for this_mock in class_mock, class_mock(): michael@0: this_mock.a() michael@0: this_mock.a.assert_called_with() michael@0: self.assertRaises(TypeError, this_mock.a, 'foo') michael@0: self.assertRaises(AttributeError, getattr, this_mock, 'b') michael@0: michael@0: instance_mock = create_autospec(Foo()) michael@0: instance_mock.a() michael@0: instance_mock.a.assert_called_with() michael@0: self.assertRaises(TypeError, instance_mock.a, 'foo') michael@0: self.assertRaises(AttributeError, getattr, instance_mock, 'b') michael@0: michael@0: # The return value isn't isn't callable michael@0: self.assertRaises(TypeError, instance_mock) michael@0: michael@0: instance_mock.Bar.f() michael@0: instance_mock.Bar.f.assert_called_with() michael@0: self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g') michael@0: michael@0: instance_mock.Bar().f() michael@0: instance_mock.Bar().f.assert_called_with() michael@0: self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g') michael@0: michael@0: michael@0: def test_inherit(self): michael@0: class Foo(object): michael@0: a = 3 michael@0: michael@0: Foo.Foo = Foo michael@0: michael@0: # class michael@0: mock = create_autospec(Foo) michael@0: instance = mock() michael@0: self.assertRaises(AttributeError, getattr, instance, 'b') michael@0: michael@0: attr_instance = mock.Foo() michael@0: self.assertRaises(AttributeError, getattr, attr_instance, 'b') michael@0: michael@0: # instance michael@0: mock = create_autospec(Foo()) michael@0: self.assertRaises(AttributeError, getattr, mock, 'b') michael@0: self.assertRaises(TypeError, mock) michael@0: michael@0: # attribute instance michael@0: call_result = mock.Foo() michael@0: self.assertRaises(AttributeError, getattr, call_result, 'b') michael@0: michael@0: michael@0: def test_builtins(self): michael@0: # used to fail with infinite recursion michael@0: create_autospec(1) michael@0: michael@0: create_autospec(int) michael@0: create_autospec('foo') michael@0: create_autospec(str) michael@0: create_autospec({}) michael@0: create_autospec(dict) michael@0: create_autospec([]) michael@0: create_autospec(list) michael@0: create_autospec(set()) michael@0: create_autospec(set) michael@0: create_autospec(1.0) michael@0: create_autospec(float) michael@0: create_autospec(1j) michael@0: create_autospec(complex) michael@0: create_autospec(False) michael@0: create_autospec(True) michael@0: michael@0: michael@0: def test_function(self): michael@0: def f(a, b): michael@0: pass michael@0: michael@0: mock = create_autospec(f) michael@0: self.assertRaises(TypeError, mock) michael@0: mock(1, 2) michael@0: mock.assert_called_with(1, 2) michael@0: michael@0: f.f = f michael@0: mock = create_autospec(f) michael@0: self.assertRaises(TypeError, mock.f) michael@0: mock.f(3, 4) michael@0: mock.f.assert_called_with(3, 4) michael@0: michael@0: michael@0: def test_skip_attributeerrors(self): michael@0: class Raiser(object): michael@0: def __get__(self, obj, type=None): michael@0: if obj is None: michael@0: raise AttributeError('Can only be accessed via an instance') michael@0: michael@0: class RaiserClass(object): michael@0: raiser = Raiser() michael@0: michael@0: @staticmethod michael@0: def existing(a, b): michael@0: return a + b michael@0: michael@0: s = create_autospec(RaiserClass) michael@0: self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3)) michael@0: s.existing(1, 2) michael@0: self.assertRaises(AttributeError, lambda: s.nonexisting) michael@0: michael@0: # check we can fetch the raiser attribute and it has no spec michael@0: obj = s.raiser michael@0: obj.foo, obj.bar michael@0: michael@0: michael@0: def test_signature_class(self): michael@0: class Foo(object): michael@0: def __init__(self, a, b=3): michael@0: pass michael@0: michael@0: mock = create_autospec(Foo) michael@0: michael@0: self.assertRaises(TypeError, mock) michael@0: mock(1) michael@0: mock.assert_called_once_with(1) michael@0: michael@0: mock(4, 5) michael@0: mock.assert_called_with(4, 5) michael@0: michael@0: michael@0: @unittest2.skipIf(inPy3k, 'no old style classes in Python 3') michael@0: def test_signature_old_style_class(self): michael@0: class Foo: michael@0: def __init__(self, a, b=3): michael@0: pass michael@0: michael@0: mock = create_autospec(Foo) michael@0: michael@0: self.assertRaises(TypeError, mock) michael@0: mock(1) michael@0: mock.assert_called_once_with(1) michael@0: michael@0: mock(4, 5) michael@0: mock.assert_called_with(4, 5) michael@0: michael@0: michael@0: def test_class_with_no_init(self): michael@0: # this used to raise an exception michael@0: # due to trying to get a signature from object.__init__ michael@0: class Foo(object): michael@0: pass michael@0: create_autospec(Foo) michael@0: michael@0: michael@0: @unittest2.skipIf(inPy3k, 'no old style classes in Python 3') michael@0: def test_old_style_class_with_no_init(self): michael@0: # this used to raise an exception michael@0: # due to Foo.__init__ raising an AttributeError michael@0: class Foo: michael@0: pass michael@0: create_autospec(Foo) michael@0: michael@0: michael@0: def test_signature_callable(self): michael@0: class Callable(object): michael@0: def __init__(self): michael@0: pass michael@0: def __call__(self, a): michael@0: pass michael@0: michael@0: mock = create_autospec(Callable) michael@0: mock() michael@0: mock.assert_called_once_with() michael@0: self.assertRaises(TypeError, mock, 'a') michael@0: michael@0: instance = mock() michael@0: self.assertRaises(TypeError, instance) michael@0: instance(a='a') michael@0: instance.assert_called_once_with(a='a') michael@0: instance('a') michael@0: instance.assert_called_with('a') michael@0: michael@0: mock = create_autospec(Callable()) michael@0: mock(a='a') michael@0: mock.assert_called_once_with(a='a') michael@0: self.assertRaises(TypeError, mock) michael@0: mock('a') michael@0: mock.assert_called_with('a') michael@0: michael@0: michael@0: def test_signature_noncallable(self): michael@0: class NonCallable(object): michael@0: def __init__(self): michael@0: pass michael@0: michael@0: mock = create_autospec(NonCallable) michael@0: instance = mock() michael@0: mock.assert_called_once_with() michael@0: self.assertRaises(TypeError, mock, 'a') michael@0: self.assertRaises(TypeError, instance) michael@0: self.assertRaises(TypeError, instance, 'a') michael@0: michael@0: mock = create_autospec(NonCallable()) michael@0: self.assertRaises(TypeError, mock) michael@0: self.assertRaises(TypeError, mock, 'a') michael@0: michael@0: michael@0: def test_create_autospec_none(self): michael@0: class Foo(object): michael@0: bar = None michael@0: michael@0: mock = create_autospec(Foo) michael@0: none = mock.bar michael@0: self.assertNotIsInstance(none, type(None)) michael@0: michael@0: none.foo() michael@0: none.foo.assert_called_once_with() michael@0: michael@0: michael@0: def test_autospec_functions_with_self_in_odd_place(self): michael@0: class Foo(object): michael@0: def f(a, self): michael@0: pass michael@0: michael@0: a = create_autospec(Foo) michael@0: a.f(self=10) michael@0: a.f.assert_called_with(self=10) michael@0: michael@0: michael@0: def test_autospec_property(self): michael@0: class Foo(object): michael@0: @property michael@0: def foo(self): michael@0: return 3 michael@0: michael@0: foo = create_autospec(Foo) michael@0: mock_property = foo.foo michael@0: michael@0: # no spec on properties michael@0: self.assertTrue(isinstance(mock_property, MagicMock)) michael@0: mock_property(1, 2, 3) michael@0: mock_property.abc(4, 5, 6) michael@0: mock_property.assert_called_once_with(1, 2, 3) michael@0: mock_property.abc.assert_called_once_with(4, 5, 6) michael@0: michael@0: michael@0: def test_autospec_slots(self): michael@0: class Foo(object): michael@0: __slots__ = ['a'] michael@0: michael@0: foo = create_autospec(Foo) michael@0: mock_slot = foo.a michael@0: michael@0: # no spec on slots michael@0: mock_slot(1, 2, 3) michael@0: mock_slot.abc(4, 5, 6) michael@0: mock_slot.assert_called_once_with(1, 2, 3) michael@0: mock_slot.abc.assert_called_once_with(4, 5, 6) michael@0: michael@0: michael@0: class TestCallList(unittest2.TestCase): michael@0: michael@0: def test_args_list_contains_call_list(self): michael@0: mock = Mock() michael@0: self.assertIsInstance(mock.call_args_list, _CallList) michael@0: michael@0: mock(1, 2) michael@0: mock(a=3) michael@0: mock(3, 4) michael@0: mock(b=6) michael@0: michael@0: for kall in call(1, 2), call(a=3), call(3, 4), call(b=6): michael@0: self.assertTrue(kall in mock.call_args_list) michael@0: michael@0: calls = [call(a=3), call(3, 4)] michael@0: self.assertTrue(calls in mock.call_args_list) michael@0: calls = [call(1, 2), call(a=3)] michael@0: self.assertTrue(calls in mock.call_args_list) michael@0: calls = [call(3, 4), call(b=6)] michael@0: self.assertTrue(calls in mock.call_args_list) michael@0: calls = [call(3, 4)] michael@0: self.assertTrue(calls in mock.call_args_list) michael@0: michael@0: self.assertFalse(call('fish') in mock.call_args_list) michael@0: self.assertFalse([call('fish')] in mock.call_args_list) michael@0: michael@0: michael@0: def test_call_list_str(self): michael@0: mock = Mock() michael@0: mock(1, 2) michael@0: mock.foo(a=3) michael@0: mock.foo.bar().baz('fish', cat='dog') michael@0: michael@0: expected = ( michael@0: "[call(1, 2),\n" michael@0: " call.foo(a=3),\n" michael@0: " call.foo.bar(),\n" michael@0: " call.foo.bar().baz('fish', cat='dog')]" michael@0: ) michael@0: self.assertEqual(str(mock.mock_calls), expected) michael@0: michael@0: michael@0: def test_propertymock(self): michael@0: p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock) michael@0: mock = p.start() michael@0: try: michael@0: SomeClass.one michael@0: mock.assert_called_once_with() michael@0: michael@0: s = SomeClass() michael@0: s.one michael@0: mock.assert_called_with() michael@0: self.assertEqual(mock.mock_calls, [call(), call()]) michael@0: michael@0: s.one = 3 michael@0: self.assertEqual(mock.mock_calls, [call(), call(), call(3)]) michael@0: finally: michael@0: p.stop() michael@0: michael@0: michael@0: def test_propertymock_returnvalue(self): michael@0: m = MagicMock() michael@0: p = PropertyMock() michael@0: type(m).foo = p michael@0: michael@0: returned = m.foo michael@0: p.assert_called_once_with() michael@0: self.assertIsInstance(returned, MagicMock) michael@0: self.assertNotIsInstance(returned, PropertyMock) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: unittest2.main()