[编辑:问题只存在于Python2.7中。在3.7版本中,它可以正常工作。有什么区别吗?有没有可能使它也适用于2.7,我出于某些原因不得不使用它]
我想用Mock覆盖socket的方法sendto
。我的第一个方法是这样的:
class TestClass01(TestCase):
@mock.patch.object(socket.socket, "sendto", new=mock_sendto)
def test_function1(self):
...
但是,这导致了一个例外:
AttributeError: '_socketobject' object attribute 'sendto' is read-only.
这种方法在其他类中工作得很好,但在socket中没有。我认为,原因是__init__
为所有成员函数调用setattr,如下所示:
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
if _sock is None:
_sock = _realsocket(family, type, proto)
self._sock = _sock
for method in _delegate_methods:
setattr(self, method, getattr(_sock, method)) <<<<<<<<<<<<<
然后,我尝试了另一种方法:
class TestClass01(TestCase):
@mock.patch("socket.socket")
def test_function1(self, mock_class):
s = mock.Mock(socket.socket)
s.sendto = mock_sendto
s.close = mock_close
mock_class.return_value = s
# ... run my tests here ...
args, kwargs = mock_class.call_args_list[0]
# Check arguments passed to sendto
不过,这是可行的,调用参数列表只给出了:
((2, 2), {})
它对应于构造函数AF_INET=2
和SOCK_DGRAM=2
。没有我调用sendto
的痕迹。我的解决方法是使用全局变量,这是我从mock_sendto
函数设置的。不过,不是很优雅。你知道吗
我有三个问题:
all_args_list
只跟踪构造的调用而不跟踪sendto的调用?你知道吗提前感谢您的回答!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐