我正在尝试为正在编写的应用程序建立测试夹具,其中我的一个类被模拟所替代。我很乐意将模拟类的大多数属性保留为默认MagicMock
实例(我只想对它们的用法进行断言),但是该类还具有我想为其提供特定返回值的属性。 。
作为参考,这是我要修补的类的概述:
class CommunicationService(object):
def __init__(self):
self.__received_response = Subject()
@property
def received_response(self):
return self.__received_response
def establish_communication(self, hostname: str, port: int) -> None:
pass
def send_request(self, request: str) -> None:
pass
我遇到的困难是,当我打补丁时CommunicationService
,我还尝试为将返回特定值PropertyMock
的received_response
属性设置a 。但是,当我在生产代码中实例化此类时,我发现对的调用CommunicationService.received_response
返回的是默认MagicMock
实例,而不是我希望它们返回的特定值。
在测试设置期间,我执行以下操作:
context.mock_comms_exit_stack = ExitStack()
context.mock_comms = context.mock_comms_exit_stack.enter_context(
patch('testcube.comms.CommunicationService', spec=True))
# Make 'received_response' observers subscribe to a mock subject.
context.mock_received_response_subject = Subject()
type(context.mock_comms).received_response = PropertyMock(return_value=context.mock_received_response_subject)
# Reload TestCube module to make it import the mock communications class.
reload_testcube_module(context)
在我的生产代码中(执行此设置后调用):
# Establish communication with TestCube Web Service.
comms = CommunicationService()
comms.establish_communication(hostname, port)
# Wire plugins with communications service.
for plugin in context.command.plugins:
plugin.on_response = comms.received_response
plugin.request_generated.subscribe(comms.send_request)
我希望comms.received_response
是Subject
(属性嘲笑的返回值)的一个实例。但是,相反,我得到以下信息:
<MagicMock name='CommunicationService().received_response' id='4580209944'>
问题似乎是从patch方法返回的实例上的模拟属性可以正常工作,但是在创建修补类的新实例时,模拟属性会变得混乱。
我相信下面的代码片段抓住了这个问题的本质。如果有一种方法可以修改下面的脚本以使其print(foo.bar)
返回mock value
,那么希望它会显示如何解决我的实际代码中的问题。
from contextlib import ExitStack
from unittest.mock import patch, PropertyMock
class Foo:
@property
def bar(self):
return 'real value'
exit_stack = ExitStack()
mock_foo = exit_stack.enter_context(patch('__main__.Foo', spec=True))
mock_bar = PropertyMock(return_value='mock value')
type(mock_foo).bar = mock_bar
print(mock_foo.bar) # 'mock value' (expected)
foo = Foo()
print(foo.bar) # <MagicMock name='Foo().bar' id='4372262080'> (unexpected - should be 'mock value')
exit_stack.close()
下一行:
type(mock_foo).bar = mock_bar
模拟到那时候mock_foo
是的返回值enter_context
。如果我对文档的理解正确,则意味着您现在正在实际处理__enter__
的返回值的结果patch('__main__.Foo', spec=True)
。
如果将该行更改为:
type(Foo.return_value).bar = mock_bar
那么您将模拟bar
实例的属性Foo
(因为调用类的返回值是一个实例)。然后,第二个打印语句将按mock value
预期打印。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句