[英]Mock/patch function in view
from flask import request
from flask.views import MethodView
from unittest.mock import patch
from router.views.time_view import (
_utcnow
)
class StateSetupView(MethodView):
@app.route("/state-setup", methods=['POST'])
def post():
print(f"Non-mocked version: {_utcnow()}")
with patch('router.views.time_view._utcnow', return_value = "LOL"):
print(f"Mocked version: {_utcnow()}")
I can't seem to mock the function return value in runtime.我似乎无法在运行时模拟 function 返回值。 The code above returns the same value in both instances.
上面的代码在两个实例中返回相同的值。 I do not want to wrap this in a pytest, this needs to work in a view.
我不想将它包装在 pytest 中,这需要在视图中工作。
Just simple replacing the function straight up worked.只需简单地更换 function 即可。
router.views.time_view._utcnow = lambda: 12346789
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.