当前位置: 首页>>代码示例>>Python>>正文


Python mock.x方法代码示例

本文整理汇总了Python中unittest.mock.x方法的典型用法代码示例。如果您正苦于以下问题:Python mock.x方法的具体用法?Python mock.x怎么用?Python mock.x使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在unittest.mock的用法示例。


在下文中一共展示了mock.x方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_method_calls_compare_easily

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import x [as 别名]
def test_method_calls_compare_easily(self):
        mock = Mock()
        mock.something()
        self.assertEqual(mock.method_calls, [('something',)])
        self.assertEqual(mock.method_calls, [('something', (), {})])

        mock = Mock()
        mock.something('different')
        self.assertEqual(mock.method_calls, [('something', ('different',))])
        self.assertEqual(mock.method_calls,
                         [('something', ('different',), {})])

        mock = Mock()
        mock.something(x=1)
        self.assertEqual(mock.method_calls, [('something', {'x': 1})])
        self.assertEqual(mock.method_calls, [('something', (), {'x': 1})])

        mock = Mock()
        mock.something('different', some='more')
        self.assertEqual(mock.method_calls, [
            ('something', ('different',), {'some': 'more'})
        ]) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:24,代码来源:testmock.py

示例2: test_from_spec

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import x [as 别名]
def test_from_spec(self):
        class Something(object):
            x = 3
            __something__ = None
            def y(self):
                pass

        def test_attributes(mock):
            # should work
            mock.x
            mock.y
            mock.__something__
            self.assertRaisesRegex(
                AttributeError,
                "Mock object has no attribute 'z'",
                getattr, mock, 'z'
            )
            self.assertRaisesRegex(
                AttributeError,
                "Mock object has no attribute '__foobar__'",
                getattr, mock, '__foobar__'
            )

        test_attributes(Mock(spec=Something))
        test_attributes(Mock(spec=Something())) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:27,代码来源:testmock.py

示例3: test_setting_attribute_with_spec_set

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import x [as 别名]
def test_setting_attribute_with_spec_set(self):
        class X(object):
            y = 3

        mock = Mock(spec=X)
        mock.x = 'foo'

        mock = Mock(spec_set=X)
        def set_attr():
            mock.x = 'foo'

        mock.y = 'foo'
        self.assertRaises(AttributeError, set_attr) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:15,代码来源:testmock.py

示例4: test_mock_call_not_equal_non_leaf_params_different

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import x [as 别名]
def test_mock_call_not_equal_non_leaf_params_different(self):
        m = Mock()
        m.foo(x=1).bar()
        # This isn't ideal, but there's no way to fix it without breaking backwards compatibility:
        self.assertEqual(m.mock_calls[1], call.foo(x=2).bar()) 
开发者ID:guohuadeng,项目名称:odoo13-x64,代码行数:7,代码来源:testmock.py

示例5: test_assert_has_calls_not_matching_spec_error

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import x [as 别名]
def test_assert_has_calls_not_matching_spec_error(self):
        def f(x=None): pass

        mock = Mock(spec=f)
        mock(1)

        with self.assertRaisesRegex(
                AssertionError,
                '^{}$'.format(
                    re.escape('Calls not found.\n'
                              'Expected: [call()]\n'
                              'Actual: [call(1)]'))) as cm:
            mock.assert_has_calls([call()])
        self.assertIsNone(cm.exception.__cause__)


        with self.assertRaisesRegex(
                AssertionError,
                '^{}$'.format(
                    re.escape(
                        'Error processing expected calls.\n'
                        "Errors: [None, TypeError('too many positional arguments')]\n"
                        "Expected: [call(), call(1, 2)]\n"
                        'Actual: [call(1)]'))) as cm:
            mock.assert_has_calls([call(), call(1, 2)])
        self.assertIsInstance(cm.exception.__cause__, TypeError) 
开发者ID:guohuadeng,项目名称:odoo13-x64,代码行数:28,代码来源:testmock.py


注:本文中的unittest.mock.x方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。