【发布时间】:2021-08-14 05:28:23
【问题描述】:
我如何在这个例子中用 write 和 close 模拟 NamedTemporaryFile:
def submit_form(self,pdf_bytes,file_path):
with NamedTemporaryFile(mode='w+b') as temp:
temp.write(pdf_bytes)
with open(temp.name, 'rb') as pdf:
file_path = self._google_cloud_storage.upload_file(file_path,pdf,"application/pdf")
pdf.close()
temp.close()
这是我的测试
@patch.object(tempfile,"NamedTemporaryFile",return_value = None)
@patch('builtins.open', new_callable=mock_open())
def test_submit_form(self):
self._payment_approval_service.submit_form(Mock(),"testfilepath.pdf")
测试结果:
TypeError: a bytes-like object is required, not 'Mock'
in submit_form
temp.write(pdf_bytes)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = (<Mock id='140604437181680'>,), kwargs = {}
@_functools.wraps(func)
def func_wrapper(*args, **kwargs):
> return func(*args, **kwargs)
E TypeError: a bytes-like object is required, not 'Mock'
这意味着 NamedTemporaryFile().write 函数没有被模拟对吗?
【问题讨论】:
标签: python pytest python-unittest