本文整理汇总了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)
示例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)
示例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')]
示例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(),
])
示例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()])
示例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()]
)
示例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()])
示例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)
示例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)
示例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