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


Python Mock.attach_mock方法代码示例

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


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

示例1: test_no_state_msg_on_create

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
 def test_no_state_msg_on_create(self):
     """should not send a state message when the widget is created"""
     comm = Mock(spec=Comm)
     send = Mock()
     comm.attach_mock(send, 'send')
     widget = UrthWidget(comm=comm)
     assert(send.call_count == 0)
开发者ID:j-towns,项目名称:declarativewidgets,代码行数:9,代码来源:test_urth_widget.py

示例2: mock_pyglet_graphics

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
def mock_pyglet_graphics(monkeypatch):
    mock_vertex_list = Mock(side_effect=vertex_list_side_effect)
    mock_graphics = Mock
    mock_graphics.vertex_list_indexed = mock_vertex_list
    monkeypatch.setattr(pyglet, 'graphics', mock_graphics)
    mock_gl = Mock()
    mock_gl.attach_mock(Mock(name='GL_TRIANGLES'), 'GL_TRIANGLES')
    monkeypatch.setattr(pyglet, 'gl', mock_gl)
开发者ID:hsharrison,项目名称:pyglet2d,代码行数:10,代码来源:test_pyglet2d.py

示例3: test_configure_faceting_version2_has_data

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
def test_configure_faceting_version2_has_data(unified_index):
    # mock out enough of the backend to get data
    indexed_models = Mock(return_value=[Model, Model])
    facet_fieldnames = Mock(_facet_fieldnames={'a': 1, 'b':2})
    facet_fieldnames.attach_mock(indexed_models, 'get_indexed_models')
    unified_index.return_value = facet_fieldnames
    form = PreSelectedModelSearchForm(data={})
    assert form.configure_faceting() == [('a', 'A'), ('b', 'B')]
开发者ID:kezabelle,项目名称:django-haystackbrowser,代码行数:10,代码来源:test_forms.py

示例4: test_with_cursor

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
    def test_with_cursor(self, seek_cursor, get_next):
        ''' test when cursor exists '''

        parent = Mock()
        parent.attach_mock(seek_cursor, 'seek_cursor')
        parent.attach_mock(get_next, 'get_next')
        self.client(cursor=sentinel.cursor)
        # seeks to the cursor and skips first
        parent.assert_has_calls([
            call.seek_cursor(sentinel.cursor),
            call.get_next(),
        ])
开发者ID:lincheney,项目名称:journald-2-cloudwatch,代码行数:14,代码来源:test_journald_client.py

示例5: test_manager_mock

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
    def test_manager_mock(self):

        class Foo(object):
            __qualname__ = 'MockTest.test_manager_mock.<locals>.Foo'
            one = 'one'
            two = 'two'

        manager = Mock()
        p1 = patch.object(Foo, 'one')
        p2 = patch.object(Foo, 'two')
        mock_one = p1.start()
        self.addCleanup(p1.stop)
        mock_two = p2.start()
        self.addCleanup(p2.stop)
        manager.attach_mock(mock_one, 'one')
        manager.attach_mock(mock_two, 'two')
        Foo.two()
        Foo.one()
        self.assertEqual(manager.mock_calls, [call.two(), call.one()])
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:21,代码来源:testmock.py

示例6: test_messages

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
    def test_messages(self):
        ''' passes logs from reader '''

        logs1 = [1, 2, 3]
        logs2 = [4, 5, 6]
        logs = logs1 + [{}] + logs2 + [{}]
        with patch('systemd.journal.Reader.get_next', side_effect=logs, spec_set=True) as get_next:
            with patch.object(self.READER, 'wait', side_effect=[None, StopIteration], spec_set=True) as wait:
                parent = Mock()
                parent.attach_mock(get_next, 'get_next')
                parent.attach_mock(wait, 'wait')

                self.assertListEqual(list(self.client()), logs)
                parent.assert_has_calls(
                    [call.get_next()] * 4 +
                    [call.wait()] +
                    [call.get_next()] * 4 +
                    [call.wait()]
                )
开发者ID:lincheney,项目名称:journald-2-cloudwatch,代码行数:21,代码来源:test_journald_client.py

示例7: test_manager_mock

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
    def test_manager_mock(self):
        class Foo(object):
            one = "one"
            two = "two"

        manager = Mock()
        p1 = patch.object(Foo, "one")
        p2 = patch.object(Foo, "two")

        mock_one = p1.start()
        self.addCleanup(p1.stop)
        mock_two = p2.start()
        self.addCleanup(p2.stop)

        manager.attach_mock(mock_one, "one")
        manager.attach_mock(mock_two, "two")

        Foo.two()
        Foo.one()

        self.assertEqual(manager.mock_calls, [call.two(), call.one()])
开发者ID:GaloisInc,项目名称:echronos,代码行数:23,代码来源:testmock.py

示例8: test_form_valid

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
    def test_form_valid(self, existing):
        instance = sqlalchemy.ModelFormMixin()
        if existing:
            instance.object = obj = Mock()
        instance.model = Mock()

        form = Mock()

        mocks = Mock()
        with patch.object(sqlalchemy, 'session') as m1:
            with patch.object(sqlalchemy.FormMixin, 'form_valid') as m2:
                with patch.object(sqlalchemy, '_touch') as m3:

                    mocks.attach_mock(m1, 'session')
                    mocks.attach_mock(form, 'form')
                    mocks.attach_mock(m3, 'touch')

                    assert instance.form_valid(form) == m2.return_value

                    m2.assert_called_once_with(form)

                    calls = [call.form.populate_obj(instance.object),
                             call.session.commit(),
                             call.touch(instance.object)]

                    if not existing:
                        assert instance.object == instance.model.return_value

                        calls.insert(0, call.session.add(instance.object))
                    else:
                        assert instance.object == obj

                    mocks.assert_has_calls(calls)
开发者ID:artisanofcode,项目名称:flask-generic-views,代码行数:35,代码来源:test_sqlalchemy.py

示例9: test_delete

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
    def test_delete(self):
        instance = sqlalchemy.DeletionMixin()
        instance.get_object = get_object = Mock()
        instance.get_success_url = get_success_url = Mock()

        mocks = Mock()

        with patch.object(sqlalchemy, 'session') as m1:
            with patch.object(sqlalchemy, 'redirect') as m2:
                with patch.object(sqlalchemy, '_touch') as m3:
                    mocks.attach_mock(m1, 'session')
                    mocks.attach_mock(m3, 'touch')

                    assert instance.delete() == m2.return_value
                    assert instance.object == get_object.return_value

                    calls = [call.session.delete(instance.object),
                             call.session.commit(),
                             call.touch(instance.object)]

                    mocks.assert_has_calls(calls)

                    m2.assert_called_once_with(get_success_url.return_value)
开发者ID:artisanofcode,项目名称:flask-generic-views,代码行数:25,代码来源:test_sqlalchemy.py

示例10: Mock

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import attach_mock [as 别名]
from haystackbrowser.forms import PreSelectedModelSearchForm
try:
    from unittest.mock import patch, Mock
except ImportError:  # < python 3.3
    from mock import patch, Mock

skip_old_haystack = pytest.mark.skipif(settings.OLD_HAYSTACK is True,
                                  reason="Doesn't apply to Haystack 1.2.x")

skip_new_haystack = pytest.mark.skipif(settings.OLD_HAYSTACK is False,
                                       reason="Doesn't apply to Haystack 2.x")


Model = Mock(return_value='testmodel')
Meta = Mock(app_label='test', model_name='testing', module_name='anothertest')
Model.attach_mock(Meta, '_meta')


@skip_new_haystack
def test_guess_haystack_version1():
    form = PreSelectedModelSearchForm(data={})
    assert form.version == 1


@skip_old_haystack
def test_guess_haystack_version2():
    form = PreSelectedModelSearchForm(data={})
    assert form.version == 2


@skip_new_haystack
开发者ID:kezabelle,项目名称:django-haystackbrowser,代码行数:33,代码来源:test_forms.py


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