[英]Monkey patch a python instance method using the original method from the class
[英]Python: Mocking an instance of a class inside a method using @patch
我在 Python 中有一个方法foo
可以创建一个服务类。 我想模拟 Service 类,但是当我运行测试时,它仍然尝试实例化该类。 这是我的设置的简化版本:
class Service:
def __init__(self, service):
self.service_stuff = service
def run_service(self):
do_service_stuff()
def foo:
new_service = Service("bar")
new_service.run_service()
然后我的单元测试:
@patch('mymodule.service_file.Service')
def test_foo(self, mock_service):
foo()
我想运行 foo,但让它使用我的模拟对象而不是创建实际的 Service 实例,但是当我运行它时,它会尝试实例化 Service() 的实际实例并像往常一样运行 foo()它似乎可以识别我放入补丁中的字符串签名。 为什么会这样?
弄清楚了:对类的补丁引用必须是方法本身中的导入名称,而不是原始类,类似于https://stackoverflow.com/a/32461515/4234853
所以补丁应该看起来像: @patch('mymodule.foo_file.Service')
而不是试图直接修补类。
在这种情况下,使您的函数更易于测试可能会更容易:
def foo(cls=Service):
new_service = cls("bar")
new_service.run_service()
那么你的测试不需要修补任何东西。
def test_foo(self):
mock_service = Mock()
foo(mock_service)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.