我正在测试两个视图,并使用FileStorage对象测试两个视图,以保存来自发帖请求的上传文件。
在测试期间,我实际上并不希望保存上传的存储,因此我使用FileStorage.save
的{{1}}装饰器修补了unittest
方法。
patch
@patch.object(FileStorage, 'save')
def test_create_data_file(self, fs_save_mock):
"""Test creation/upload of a data data file."""
file_storage = FileStorage(
name='Test Storage', filename='teststorage.ext')
data = {'file': file_storage}
rv = self.client.post('/api/data/upload', data=data)
json_data = rv.get_json()
datafile = DataFile.query.get(json_data['id'])
fs_save_mock.assert_called_once_with(
generate_upload_filepath(file_storage.filename))
@patch.object(FileStorage, 'save')
def test_create_data_file(self, fs_save_mock):
"""Test creation/upload of a data data file."""
file_storage = FileStorage(
name='Test Storage', filename='teststorage.ext')
data = {'file': file_storage}
rv = self.client.post('/api/data/upload', data=data)
json_data = rv.get_json()
datafile = DataFile.query.get(json_data['id'])
fs_save_mock.assert_called_once_with(
generate_upload_filepath(file_storage.filename))
如果仅运行其中一个测试,则运行良好,但是添加第二个测试时,运行失败并显示以下错误:
@blueprint.route('/upload', methods=['POST'])
@use_kwargs({'file_storage': fields.Field(location='files', load_from='file')})
def upload_datafile(file_storage):
"""Upload a datafile."""
if file_storage is missing:
abort(400, 'No file found in the request.')
filepath = generate_upload_filepath(file_storage.filename)
file_storage.save(filepath)
data_file = DataFile.create(filename=file_storage.filename, path=filepath)
return jsonify(DataFileSchema().dump(data_file).data), 201
不确定如何解决此问题,你们中的任何人都可以帮我解决这个问题吗?