當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。