Hi,
I am trying to mock a function : test_func but when i run the test, it don’t take mock values.
please let me know if you need more info. any suggestions are appreciated.
Below is my project structure :
|_my_api
| _|helper
| __|function_alpha.py
|_my_api.py
|tests
||_test_my_api.py
------------------- test_my_api-.py-----------------------
from unittest import mock
from contextlib import contextmanager
from my_api.my_api import app
import json
@contextmanager
def mocked_bad_event_es():
with mock.patch("my_api.helper.function_alpha.test_func") as mocked_query_single_event:
def _mock_query_single_event(_host, _index, _sort_field: str, identifier: str, **kwargs):
json({"applicationId": 'Mock_test',
"events": []
})
mocked_query_single_event.side_effect = _mock_query_single_event
yield
def test_xyz():
with mocked_bad_event_es():
app.test_client
_, response = app.test_client.get("/protected/20503/events", params=[("applicationId", "sampleApplicationId")])
#unwrap the response
response_body = json.loads(response.body.decode('utf-8'))
import pdb; pdb.set_trace()
assert response.status == 200
assert len(response_body['events']) == 0
— my_api.py ----------
@app.route('/protected/20503/events')
async def lookup_application_id(request):
<removed some code>
api_response = await test_func(app.ctx.httpSession, app.config, application_id, limit)
return api_response