我想用mock替换类中的方法:
from unittest.mock import patch
class A(object):
def method(self, string):
print(self, "method", string)
def method2(self, string):
print(self, "method2", string)
with patch.object(A, 'method', side_effect=method2):
a = A()
a.method("string")
a.method.assert_called_with("string")
Run Code Online (Sandbox Code Playgroud)
......但是我被电脑侮辱了:
TypeError: method2() missing 1 required positional argument: 'string'
Run Code Online (Sandbox Code Playgroud) 当从pubnub接收消息时,没有关于发送者的信息.如何知道来自visitorA或visitorB的消息?在网络上有一些例子,发送者用信息发送他的名字,但是如何知道他不是在欺骗别人的身份?
这是一个聊天界面的示例:
<html>
<body>
<form id="message_form">
<input id="message_input" type="text"/>
</form>
<div id="chat"></div>
<script src="http://cdn.pubnub.com/pubnub-3.7.1.min.js"></script>
<script>
var pubnub = PUBNUB.init({
publish_key: 'demo',
subscribe_key: 'demo'
});
pubnub.subscribe({
channel: 'chat',
message: function(message){
var div = document.createElement("div");
div.textContent = message;
var chat = document.getElementById("chat");
chat.appendChild(div);
}
});
var form = document.getElementById("message_form");
form.onsubmit = function(e) {
var input = document.getElementById("message_input");
pubnub.publish({
channel: 'chat',
message: input.value
});
input.value = '';
e.preventDefault();
};
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)