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


Python mock.assert_called_with方法代码示例

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


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

示例1: assert_called_with

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def assert_called_with(_mock_self, *args, **kwargs):
        """assert that the mock was called with the specified arguments.

        Raises an AssertionError if the args and keyword args passed in are
        different to the last call to the mock."""
        self = _mock_self
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            raise AssertionError('Expected call: %s\nNot called' % (expected,))

        def _error_message(cause):
            msg = self._format_mock_failure_message(args, kwargs)
            if six.PY2 and cause is not None:
                # Tack on some diagnostics for Python without __cause__
                msg = '%s\n%s' % (msg, str(cause))
            return msg
        expected = self._call_matcher((args, kwargs))
        actual = self._call_matcher(self.call_args)
        if expected != actual:
            cause = expected if isinstance(expected, Exception) else None
            six.raise_from(AssertionError(_error_message(cause)), cause) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:23,代码来源:mock.py

示例2: test_assert_called_with_function_spec

# 需要导入模块: import mock [as 别名]
# 或者: from 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)
        if hasattr(cm.exception, '__cause__'):
            self.assertIsInstance(cm.exception.__cause__, TypeError) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:18,代码来源:testmock.py

示例3: test_assert_called_with_method_spec

# 需要导入模块: import mock [as 别名]
# 或者: from 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:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:20,代码来源:testmock.py

示例4: test_scale_new_app_instances_up_50_percent

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def test_scale_new_app_instances_up_50_percent(self, mock):
        """When scaling new_app instances, increase instances by 50% of
           existing instances if we have not yet met or surpassed the amount
           of instances deployed by old_app
        """
        def run(args):
            args.initial_instances = 5
            new_app = {
                'instances': 10,
                'labels': {
                    'HAPROXY_DEPLOYMENT_TARGET_INSTANCES': 30
                }
            }
            old_app = {'instances': 30}
            zdd.scale_new_app_instances(args, new_app, old_app)
            mock.assert_called_with(
                args, new_app, 15)
        for a in _arg_cases():
            run(a) 
开发者ID:mesosphere,项目名称:marathon-lb,代码行数:21,代码来源:test_zdd.py

示例5: test_scale_new_app_instances_to_target

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def test_scale_new_app_instances_to_target(self, mock):
        """When scaling new instances up, if we have met or surpassed the
           amount of instances deployed for old_app, go right to our
           deployment target amount of instances for new_app
        """
        def run(args):
            new_app = {
                'instances': 10,
                'labels': {
                    'HAPROXY_DEPLOYMENT_TARGET_INSTANCES': 30
                }
            }
            old_app = {'instances': 8}
            args.initial_instances = 5
            zdd.scale_new_app_instances(args, new_app, old_app)
            mock.assert_called_with(
                args, new_app, 30)
        for a in _arg_cases():
            run(a) 
开发者ID:mesosphere,项目名称:marathon-lb,代码行数:21,代码来源:test_zdd.py

示例6: test_scale_new_app_instances_hybrid

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def test_scale_new_app_instances_hybrid(self, mock):
        """When scaling new instances up, if we have met or surpassed the
           amount of instances deployed for old_app, go right to our
           deployment target amount of instances for new_app
        """
        def run(args):
            new_app = {
                'instances': 10,
                'labels': {
                    'HAPROXY_DEPLOYMENT_NEW_INSTANCES': 15,
                    'HAPROXY_DEPLOYMENT_TARGET_INSTANCES': 30
                }
            }
            old_app = {'instances': 20}
            args.initial_instances = 5
            zdd.scale_new_app_instances(args, new_app, old_app)
            mock.assert_called_with(
                args, new_app, 15)
        for a in _arg_cases():
            run(a) 
开发者ID:mesosphere,项目名称:marathon-lb,代码行数:22,代码来源:test_zdd.py

示例7: test_pre_kill_hook

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def test_pre_kill_hook(self, mock):
        # TODO(BM): This test is naive. An end-to-end test would be nice.
        def run(args):
            args.pre_kill_hook = 'myhook'
            old_app = {
                'id': 'oldApp'
            }
            new_app = {
                'id': 'newApp'
            }
            tasks_to_kill = ['task1', 'task2']
            zdd.execute_pre_kill_hook(args,
                                      old_app,
                                      tasks_to_kill,
                                      new_app)
            mock.assert_called_with([args.pre_kill_hook,
                                     '{"id": "oldApp"}',
                                     '["task1", "task2"]',
                                     '{"id": "newApp"}'])
        for a in _arg_cases():
            run(a) 
开发者ID:mesosphere,项目名称:marathon-lb,代码行数:23,代码来源:test_zdd.py

示例8: test__get_resource_from_id

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def test__get_resource_from_id(self):
        id = "eye dee"
        value = "hello"
        attrs = {"first": "Brian", "last": "Curtin"}

        # The isinstance check needs to take a type, not an instance,
        # so the mock.assert_called_with method isn't helpful here since
        # we can't pass in a mocked object. This class is a crude version
        # of that same behavior to let us check that `new` gets
        # called with the expected arguments.

        class Fake:
            call = {}

            @classmethod
            def new(cls, **kwargs):
                cls.call = kwargs
                return value

        result = self.fake_proxy._get_resource(Fake, id, **attrs)

        self.assertDictEqual(
            dict(id=id, connection=mock.ANY, **attrs), Fake.call)
        self.assertEqual(value, result) 
开发者ID:openstack,项目名称:openstacksdk,代码行数:26,代码来源:test_proxy.py

示例9: assert_called_with

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def assert_called_with(_mock_self, *args, **kwargs):
        """assert that the mock was called with the specified arguments.

        Raises an AssertionError if the args and keyword args passed in are
        different to the last call to the mock."""
        self = _mock_self
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            actual = 'not called.'
            error_message = ('expected call not found.\nExpected: %s\nActual: %s'
                             % (expected, actual))
            raise AssertionError(error_message)

        def _error_message(cause):
            msg = self._format_mock_failure_message(args, kwargs)
            if six.PY2 and cause is not None:
                # Tack on some diagnostics for Python without __cause__
                msg = '{}\n{}'.format(msg, str(cause))
            return msg
        expected = self._call_matcher((args, kwargs))
        actual = self._call_matcher(self.call_args)
        if expected != actual:
            cause = expected if isinstance(expected, Exception) else None
            six.raise_from(AssertionError(_error_message(cause)), cause) 
开发者ID:ali5h,项目名称:rules_pip,代码行数:26,代码来源:mock.py

示例10: assert_called_with

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def assert_called_with(_mock_self, *args, **kwargs):
        """assert that the mock was called with the specified arguments.

        Raises an AssertionError if the args and keyword args passed in are
        different to the last call to the mock."""
        self = _mock_self
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            raise AssertionError('Expected call: %s\nNot called' % (expected,))

        def _error_message(cause):
            msg = self._format_mock_failure_message(args, kwargs)
            if not inPy3k and cause is not None:
                # Tack on some diagnostics for Python without __cause__
                msg = '%s\n%s' % (msg, str(cause))
            return msg
        expected = self._call_matcher((args, kwargs))
        actual = self._call_matcher(self.call_args)
        if expected != actual:
            cause = expected if isinstance(expected, Exception) else None
            six.raise_from(AssertionError(_error_message(cause)), cause) 
开发者ID:pfchai,项目名称:ImageFusion,代码行数:23,代码来源:mock.py

示例11: _setup_func

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def _setup_func(funcopy, mock):
    funcopy.mock = mock

    # can't use isinstance with mocks
    if not _is_instance_mock(mock):
        return

    def assert_called_with(*args, **kwargs):
        return mock.assert_called_with(*args, **kwargs)
    def assert_called_once_with(*args, **kwargs):
        return mock.assert_called_once_with(*args, **kwargs)
    def assert_has_calls(*args, **kwargs):
        return mock.assert_has_calls(*args, **kwargs)
    def assert_any_call(*args, **kwargs):
        return mock.assert_any_call(*args, **kwargs)
    def reset_mock():
        funcopy.method_calls = _CallList()
        funcopy.mock_calls = _CallList()
        mock.reset_mock()
        ret = funcopy.return_value
        if _is_instance_mock(ret) and not ret is mock:
            ret.reset_mock()

    funcopy.called = False
    funcopy.call_count = 0
    funcopy.call_args = None
    funcopy.call_args_list = _CallList()
    funcopy.method_calls = _CallList()
    funcopy.mock_calls = _CallList()

    funcopy.return_value = mock.return_value
    funcopy.side_effect = mock.side_effect
    funcopy._mock_children = mock._mock_children

    funcopy.assert_called_with = assert_called_with
    funcopy.assert_called_once_with = assert_called_once_with
    funcopy.assert_has_calls = assert_has_calls
    funcopy.assert_any_call = assert_any_call
    funcopy.reset_mock = reset_mock

    mock._mock_delegate = funcopy 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:43,代码来源:mock.py

示例12: assert_called_once_with

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def assert_called_once_with(_mock_self, *args, **kwargs):
        """assert that the mock was called exactly once and with the specified
        arguments."""
        self = _mock_self
        if not self.call_count == 1:
            msg = ("Expected '%s' to be called once. Called %s times." %
                   (self._mock_name or 'mock', self.call_count))
            raise AssertionError(msg)
        return self.assert_called_with(*args, **kwargs) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:11,代码来源:mock.py

示例13: assert_any_call

# 需要导入模块: import mock [as 别名]
# 或者: from mock import assert_called_with [as 别名]
def assert_any_call(self, *args, **kwargs):
        """assert the mock has been called with the specified arguments.

        The assert passes if the mock has *ever* been called, unlike
        `assert_called_with` and `assert_called_once_with` that only pass if
        the call is the most recent one."""
        expected = self._call_matcher((args, kwargs))
        actual = [self._call_matcher(c) for c in self.call_args_list]
        if expected not in actual:
            cause = expected if isinstance(expected, Exception) else None
            expected_string = self._format_mock_call_signature(args, kwargs)
            six.raise_from(AssertionError(
                '%s call not found' % expected_string
            ), cause) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:16,代码来源:mock.py

示例14: test_side_effect

# 需要导入模块: import mock [as 别名]
# 或者: from 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:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:28,代码来源:testmock.py

示例15: test_assert_called_with

# 需要导入模块: import mock [as 别名]
# 或者: from 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:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:15,代码来源:testmock.py


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