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


Python Mock.write方法代码示例

本文整理汇总了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")
开发者ID:samirelanduk,项目名称:molecupy,代码行数:12,代码来源:test_utilities.py

示例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))
开发者ID:Alf-11,项目名称:autobahn-python,代码行数:44,代码来源:test_asyncio_rawsocket.py

示例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')
开发者ID:dduong42,项目名称:Booksee,代码行数:13,代码来源:test_downloadedfile.py

示例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")
开发者ID:samirelanduk,项目名称:molecupy,代码行数:13,代码来源:test_utilities.py

示例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)
开发者ID:samirelanduk,项目名称:pychats,代码行数:14,代码来源:test_chatlogs.py

示例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')
开发者ID:dduong42,项目名称:Booksee,代码行数:20,代码来源:test_downloadedfile.py

示例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
开发者ID:tiagocoutinho,项目名称:bliss,代码行数:24,代码来源:test_scpi.py


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