當前位置: 首頁>>代碼示例>>Python>>正文


Python mock.assert_called_with方法代碼示例

本文整理匯總了Python中unittest.mock.assert_called_with方法的典型用法代碼示例。如果您正苦於以下問題:Python mock.assert_called_with方法的具體用法?Python mock.assert_called_with怎麽用?Python mock.assert_called_with使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在unittest.mock的用法示例。


在下文中一共展示了mock.assert_called_with方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_assert_called_with_method_spec

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_assert_called_with_method_spec(self):
        def _check(mock):
            mock(1, b=2, c=3)
            mock.assert_called_with(1, 2, 3)
            mock.assert_called_with(a=1, b=2, c=3)
            self.assertRaises(AssertionError, mock.assert_called_with,
                              1, b=3, c=2)

        mock = Mock(spec=Something().meth)
        _check(mock)
        mock = Mock(spec=Something.cmeth)
        _check(mock)
        mock = Mock(spec=Something().cmeth)
        _check(mock)
        mock = Mock(spec=Something.smeth)
        _check(mock)
        mock = Mock(spec=Something().smeth)
        _check(mock) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:20,代碼來源:testmock.py

示例2: test_before_test_called_before_user_setup

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_before_test_called_before_user_setup(self):
        mock = unittest.mock.Mock()
        setattr(asynctest._fail_on._fail_on, "before_test_default", mock)
        self.addCleanup(delattr, asynctest._fail_on._fail_on,
                        "before_test_default")

        class TestCase(asynctest.TestCase):
            def setUp(self):
                self.assertTrue(mock.called)

            def runTest(self):
                pass

        for method in self.run_methods:
            with self.subTest(method=method):
                case = Test.FooTestCase()
                getattr(case, method)()

                self.assert_checked("default")
                self.assertTrue(mock.called)
                mock.assert_called_with(case) 
開發者ID:Martiusweb,項目名稱:asynctest,代碼行數:23,代碼來源:test_case.py

示例3: test_side_effect

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_side_effect(self):
        mock = Mock()

        def effect(*args, **kwargs):
            raise SystemError('kablooie')

        mock.side_effect = effect
        self.assertRaises(SystemError, mock, 1, 2, fish=3)
        mock.assert_called_with(1, 2, fish=3)

        results = [1, 2, 3]
        def effect():
            return results.pop()
        mock.side_effect = effect

        self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
                          "side effect not used correctly")

        mock = Mock(side_effect=sentinel.SideEffect)
        self.assertEqual(mock.side_effect, sentinel.SideEffect,
                          "side effect in constructor not used")

        def side_effect():
            return DEFAULT
        mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
        self.assertEqual(mock(), sentinel.RETURN) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:28,代碼來源:testmock.py

示例4: test_java_exception_side_effect

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_java_exception_side_effect(self):
        import java
        mock = Mock(side_effect=java.lang.RuntimeException("Boom!"))

        # can't use assertRaises with java exceptions
        try:
            mock(1, 2, fish=3)
        except java.lang.RuntimeException:
            pass
        else:
            self.fail('java exception not raised')
        mock.assert_called_with(1,2, fish=3) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:14,代碼來源:testmock.py

示例5: test_assert_called_with

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_assert_called_with(self):
        mock = Mock()
        mock()

        # Will raise an exception if it fails
        mock.assert_called_with()
        self.assertRaises(AssertionError, mock.assert_called_with, 1)

        mock.reset_mock()
        self.assertRaises(AssertionError, mock.assert_called_with)

        mock(1, 2, 3, a='fish', b='nothing')
        mock.assert_called_with(1, 2, 3, a='fish', b='nothing') 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:15,代碼來源:testmock.py

示例6: test_wraps_calls

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_wraps_calls(self):
        real = Mock()

        mock = Mock(wraps=real)
        self.assertEqual(mock(), real())

        real.reset_mock()

        mock(1, 2, fish=3)
        real.assert_called_with(1, 2, fish=3) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:12,代碼來源:testmock.py

示例7: test_wraps_attributes

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_wraps_attributes(self):
        class Real(object):
            attribute = Mock()

        real = Real()

        mock = Mock(wraps=real)
        self.assertEqual(mock.attribute(), real.attribute())
        self.assertRaises(AttributeError, lambda: mock.fish)

        self.assertNotEqual(mock.attribute, real.attribute)
        result = mock.attribute.frog(1, 2, fish=3)
        Real.attribute.frog.assert_called_with(1, 2, fish=3)
        self.assertEqual(result, Real.attribute.frog()) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:16,代碼來源:testmock.py

示例8: test_spec_list_subclass

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_spec_list_subclass(self):
        class Sub(list):
            pass
        mock = Mock(spec=Sub(['foo']))

        mock.append(3)
        mock.append.assert_called_with(3)
        self.assertRaises(AttributeError, getattr, mock, 'foo') 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:10,代碼來源:testmock.py

示例9: test_setting_call

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_setting_call(self):
        mock = Mock()
        def __call__(self, a):
            return self._mock_call(a)

        type(mock).__call__ = __call__
        mock('one')
        mock.assert_called_with('one')

        self.assertRaises(TypeError, mock, 'one', 'two') 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:12,代碼來源:testmock.py

示例10: test_060_vm_property_reset

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_060_vm_property_reset(self):
        with unittest.mock.patch('qubes.property.__delete__') as mock:
            value = self.call_mgmt_func(b'admin.vm.property.Reset', b'test-vm1',
                b'default_user')
            mock.assert_called_with(self.vm)
        self.assertIsNone(value)
        self.app.save.assert_called_once_with() 
開發者ID:QubesOS,項目名稱:qubes-core-admin,代碼行數:9,代碼來源:api_admin.py

示例11: test_450_property_reset

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_450_property_reset(self):
        # actual function tested for admin.vm.property.* already
        with unittest.mock.patch('qubes.property.__delete__') as mock:
            value = self.call_mgmt_func(b'admin.property.Reset', b'dom0',
                b'clockvm')
            mock.assert_called_with(self.app)
        self.assertIsNone(value)
        self.app.save.assert_called_once_with() 
開發者ID:QubesOS,項目名稱:qubes-core-admin,代碼行數:10,代碼來源:api_admin.py

示例12: test_621_backup_execute_passphrase_service

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_621_backup_execute_passphrase_service(self, mock_backup):
        backup_profile = (
            'include:\n'
            ' - test-vm1\n'
            'destination_vm: test-vm1\n'
            'destination_path: /home/user\n'
            'passphrase_vm: test-vm1\n'
        )

        @asyncio.coroutine
        def service_passphrase(*args, **kwargs):
            return (b'pass-from-vm', None)

        mock_backup.return_value.backup_do.side_effect = self.dummy_coro
        self.vm.run_service_for_stdio = unittest.mock.Mock(
            side_effect=service_passphrase)
        with tempfile.TemporaryDirectory() as profile_dir:
            with open(os.path.join(profile_dir, 'testprofile.conf'), 'w') as \
                    profile_file:
                profile_file.write(backup_profile)
            with unittest.mock.patch('qubes.config.backup_profile_dir',
                    profile_dir):
                result = self.call_mgmt_func(b'admin.backup.Execute', b'dom0',
                        b'testprofile')
        self.assertIsNone(result)
        mock_backup.assert_called_once_with(
            self.app,
            {self.vm},
            set(),
            target_vm=self.vm,
            target_dir='/home/user',
            compressed=True,
            passphrase=b'pass-from-vm')
        mock_backup.return_value.backup_do.assert_called_once_with()
        self.vm.run_service_for_stdio.assert_called_with(
            'qubes.BackupPassphrase+testprofile') 
開發者ID:QubesOS,項目名稱:qubes-core-admin,代碼行數:38,代碼來源:api_admin.py

示例13: test_assert_called_with_function_spec

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_assert_called_with_function_spec(self):
        def f(a, b, c, d=None):
            pass

        mock = Mock(spec=f)

        mock(1, b=2, c=3)
        mock.assert_called_with(1, 2, 3)
        mock.assert_called_with(a=1, b=2, c=3)
        self.assertRaises(AssertionError, mock.assert_called_with,
                          1, b=3, c=2)
        # Expected call doesn't match the spec's signature
        with self.assertRaises(AssertionError) as cm:
            mock.assert_called_with(e=8)
        self.assertIsInstance(cm.exception.__cause__, TypeError) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:17,代碼來源:testmock.py

示例14: test_assert_called_with_message

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_assert_called_with_message(self):
        mock = Mock()
        self.assertRaisesRegex(AssertionError, 'Not called',
                                mock.assert_called_with) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:6,代碼來源:testmock.py

示例15: test_assert_called_with_any

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_with [as 別名]
def test_assert_called_with_any(self):
        m = MagicMock()
        m(MagicMock())
        m.assert_called_with(mock.ANY) 
開發者ID:CedricGuillemet,項目名稱:Imogen,代碼行數:6,代碼來源:testmock.py


注:本文中的unittest.mock.assert_called_with方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。