我想模拟套接字的recv函数,并且遇到麻烦。即使我没有必要,也会遇到实际连接套接字所需的错误。
以下是所有相关代码:
interp_saliva_002_x <- as.tibble(matrix(, nrow = 131, ncol = 1))
interp_sequence <- as.numeric(seq(2,15,.1))
interp_saliva_002_x[1] <- interp_sequence
colnames(interp_saliva_002_x)[1] <- "saliva_conc"
test <- left_join(interp_saliva_002_x, interp_saliva_002, by "saliva_conc")
Pytest代码;
Class A:
def __init__.py(self):
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def connect(self):
self.s.connect((host,port))
def data_collection(self, d):
self.send_over_socket(self.cg.get_privilege_command())
self.send_over_socket(self.status_string)
buf = ""
while 1:
buf = buf + self.s.recv(self.BUFFER_SIZE).decode()
#Removed string manipulation below
self.publish(data from string manipulation)
def send_over_socket(self,data):
return (self.s.send(bytes(data.encode())))
答案 0 :(得分:0)
您缺少return_value
的{{1}};您需要解决mock_socket
将要返回的内容。您的测试应如下所示:
socket.socket
请注意,我必须在您的类代码中修复一些语法错误,并且我使def test_data_collection():
with mock.patch('socket.socket') as mock_socket:
A = classA()
mock_socket.return_value.recv.decode.return_value = "ABC123"
buf = A.data_collection()
assert "ABC123" == buf
返回了data_collection
变量,以便可以对其进行测试。
编辑:具有以下类别的文件:
buf