MCPcopy Create free account
hub / github.com/python/cpython / test_mock_add_spec

Method test_mock_add_spec

Lib/test/test_unittest/testmock/testmock.py:1907–1957  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1905 self.assertEqual(m.f.side_effect, None)
1906
1907 def test_mock_add_spec(self):
1908 class _One(object):
1909 one = 1
1910 class _Two(object):
1911 two = 2
1912 class Anything(object):
1913 one = two = three = 'four'
1914
1915 klasses = [
1916 Mock, MagicMock, NonCallableMock, NonCallableMagicMock
1917 ]
1918 for Klass in list(klasses):
1919 klasses.append(lambda K=Klass: K(spec=Anything))
1920 klasses.append(lambda K=Klass: K(spec_set=Anything))
1921
1922 for Klass in klasses:
1923 for kwargs in dict(), dict(spec_set=True):
1924 mock = Klass()
1925 #no error
1926 mock.one, mock.two, mock.three
1927
1928 for One, Two in [(_One, _Two), (['one'], ['two'])]:
1929 for kwargs in dict(), dict(spec_set=True):
1930 mock.mock_add_spec(One, **kwargs)
1931
1932 mock.one
1933 self.assertRaises(
1934 AttributeError, getattr, mock, 'two'
1935 )
1936 self.assertRaises(
1937 AttributeError, getattr, mock, 'three'
1938 )
1939 if 'spec_set' in kwargs:
1940 self.assertRaises(
1941 AttributeError, setattr, mock, 'three', None
1942 )
1943
1944 mock.mock_add_spec(Two, **kwargs)
1945 self.assertRaises(
1946 AttributeError, getattr, mock, 'one'
1947 )
1948 mock.two
1949 self.assertRaises(
1950 AttributeError, getattr, mock, 'three'
1951 )
1952 if 'spec_set' in kwargs:
1953 self.assertRaises(
1954 AttributeError, setattr, mock, 'three', None
1955 )
1956 # note that creating a mock, setting an instance attribute, and
1957 # *then* setting a spec doesn't work. Not the intended use case
1958
1959
1960 def test_mock_add_spec_magic_methods(self):

Callers

nothing calls this directly

Calls 5

listClass · 0.85
KClass · 0.50
appendMethod · 0.45
mock_add_specMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected