- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 unittest
来测试 Flask 应用程序,它是为 Python 3.5 编写的。我有一个 static
目录,其中包含将由我们的客户嵌入的 HTML 文件。根据请求来自哪个客户端,我添加了不同的 header (CORS、CSP 等)。
端点看起来像这样:
@app.route('/embed/<path:filename>', methods=['GET']):
def embed_static(filename):
response = flask.send_from_directory(app.config['EMBED_DIR'], filename)
# Do the header magic here
return response
……我为它写了一个测试:
def test_embed_static(self):
with unittest.mock.patch('flask.send_from_directory') as mocked:
page = app.test_client().get('/embed/test/index.html')
self.assertTrue(mocked.called)
最后一个断言总是失败。 unittest.mock.patch
在其他测试中就像魅力一样。
我是不是忽略了什么,或者这是我不知道的特例?
最佳答案
在进行测试 api 调用之前,您需要分配模拟对象的 return_value
。将您的代码更改为以下内容后,它就可以工作了:
def test_embed_static(self):
with unittest.mock.patch('flask.send_from_directory') as mocked:
mocked.return_value = "Test Mock"
page = app.test_client().get('/embed/test/index.html')
self.assertTrue(mocked.called)
关于python - 模拟 Flask 的 `send_from_directory` 用于测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42208195/
我需要渲染模板并动态加载图表,我的问题是,如果我有两次相同的请求名称但不同的图表,它总是显示相同的图表,因为 render_template 和 send_from_file 位于不同的 route
我正在尝试使用 send_from_directory 返回文件。此代码工作正常。 @app.route("/img/") @login_required def send_img(filename)
我有一个 Flask 网络应用程序,它允许用户上传文件,然后在浏览器中下载或显示它们。我应该使用 send_from_directory在生产时从我的服务器上的上传文件夹中提供这些文件? 最佳答案 你
我有一个用于家庭网络的小型 Flask 应用程序。 目前我让 Flask 与 uWSGI 和 nginx 一起运行。 该应用程序基本上会扫描一个位置并提供媒体文件。下面是渲染这些文件的代码: @app
在 html 表单中,我让用户选择一个文件名,然后我尝试将其发送到他的浏览器。这是代码: filename = request.form.get('filename') filepath = '/ho
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在使用 unittest 来测试 Flask 应用程序,它是为 Python 3.5 编写的。我有一个 static 目录,其中包含将由我们的客户嵌入的 HTML 文件。根据请求来自哪个客户端,我
我有一个像这样引用静态对象的 html 文件 因此浏览器尝试调用它,而 flask 失败了 http://127.0.0.1:5000/img/snacks.png 多个文件中有很多此类引用,因
我有这个图片网址: /book/cover/Computer_Science.png 但是图片所在的位置居然存在 /uploads/img/Computer_Science.png 我正在使用 Gin
我有一个基于 Flask 的网站,用户可以在其中下载一些 PDF 文件。 使用 Flask 的 send_file() 很容易实现和 send_from_directory() . 例如: @app.
当我在 Azure 上部署 Flask 应用程序时, View 引发 TypeError: send_from_directory() missing 1 required positional ar
我是一名优秀的程序员,十分优秀!