本文整理汇总了Python中unittest.mock.Mock.write方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.write方法的具体用法?Python Mock.write怎么用?Python Mock.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.write方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_saves_string_to_file
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def test_saves_string_to_file(self, mock_open):
open_return = MagicMock()
mock_file = Mock()
mock_write = MagicMock()
mock_file.write = mock_write
open_return.__enter__.return_value = mock_file
mock_open.return_value = open_return
save("filestring", "filename")
mock_open.assert_called_once_with("filename", "w")
mock_write.assert_called_once_with("filestring")
示例2: test_wamp
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def test_wamp(self):
transport = Mock(spec_set=('abort', 'close', 'write', 'get_extra_info'))
transport.write = Mock(side_effect=lambda m: messages.append(m))
client = Mock(spec=['onOpen', 'onMessage'])
def fact():
return client
messages = []
proto = WampRawSocketClientFactory(fact)()
proto.connection_made(transport)
self.assertTrue(proto._serializer)
s = proto._serializer.RAWSOCKET_SERIALIZER_ID
proto.data_received(bytes(bytearray([0x7F, 0xF0 | s, 0, 0])))
client.onOpen.assert_called_once_with(proto)
proto.send(message.Abort(u'close'))
for d in messages[1:]:
proto.data_received(d)
self.assertTrue(client.onMessage.called)
self.assertTrue(isinstance(client.onMessage.call_args[0][0], message.Abort))
# server
transport = Mock(spec_set=('abort', 'close', 'write', 'get_extra_info'))
transport.write = Mock(side_effect=lambda m: messages.append(m))
client = None
server = Mock(spec=['onOpen', 'onMessage'])
def fact_server():
return server
messages = []
proto = WampRawSocketServerFactory(fact_server)()
proto.connection_made(transport)
self.assertTrue(proto.factory._serializers)
s = proto.factory._serializers[1].RAWSOCKET_SERIALIZER_ID
proto.data_received(bytes(bytearray([0x7F, 0xF0 | s, 0, 0])))
self.assertTrue(proto._serializer)
server.onOpen.assert_called_once_with(proto)
proto.send(message.Abort(u'close'))
for d in messages[1:]:
proto.data_received(d)
self.assertTrue(server.onMessage.called)
self.assertTrue(isinstance(server.onMessage.call_args[0][0], message.Abort))
示例3: test_write
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def test_write(self):
f = Mock()
f.write = Mock()
downloaded_file = DownloadedFile(
content=b'content',
default_path='my_path',
io_factory=Mock(),
)
downloaded_file.write(f)
f.write.assert_called_once_with(b'content')
示例4: test_saves_bytestring_to_file
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def test_saves_bytestring_to_file(self, mock_open):
open_return = MagicMock()
mock_file = Mock()
mock_write = MagicMock()
mock_file.write = mock_write
open_return.__enter__.return_value = mock_file
mock_open.return_value = open_return
mock_write.side_effect = [Exception, None]
save(b"filestring", "filename")
mock_open.assert_called_with("filename", "wb")
mock_write.assert_called_with(b"filestring")
示例5: test_loading_from_json_file
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def test_loading_from_json_file(self, mock_open, mock_dump, mock_json):
open_return = MagicMock()
mock_file = Mock()
mock_write = MagicMock()
mock_file.write = mock_write
open_return.__enter__.return_value = mock_file
mock_open.return_value = open_return
log = ChatLog("Test")
mock_json.return_value = {"a": [{}]}
log.save("path/to/file")
mock_open.assert_called_once_with("path/to/file", "w")
mock_dump.assert_called_with({"a": [{}]}, mock_file)
示例6: test_save_with_arg
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def test_save_with_arg(self):
f = Mock()
f.write = Mock()
ctx = Mock()
ctx.__enter__ = Mock(return_value=f)
ctx.__exit__ = Mock(return_value=False)
io_factory = Mock(return_value=ctx)
downloaded_file = DownloadedFile(
content=b'content',
default_path='my_path',
io_factory=io_factory,
)
downloaded_file.save('new_path')
io_factory.assert_called_once_with('new_path', 'wb')
f.write.assert_called_once_with(b'content')
示例7: interface
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import write [as 别名]
def interface():
mock = Mock(_eol='\n')
mock.idn = 'BLISS INSTRUMENTS INC.,6485,123456,B04'
mock.meas = 1.2345
mock.idn_obj = dict(zip(('manufacturer', 'model', 'serial', 'version'), mock.idn.split(',')))
mock.values = {
'*IDN?': mock.idn,
'MEAS?': '%EA' % mock.meas,
}
mock.commands = []
def write_readline(msg):
return mock.values[msg.rstrip(mock._eol).upper()]
mock.write_readline = write_readline
def write_readlines(msg, n):
msgs = [msg for submsg in msg.splitlines() for msg in submsg.split(';')]
reply = [mock.values[m.upper()] for m in msgs if '?' in m]
return reply[:n]
mock.write_readlines = write_readlines
def write(msg):
mock.commands.append(msg)
mock.write = write
return mock