Dmitriy_kzn
Dmitriy_kzn

Reputation: 578

pytest @patch var inside route in FastApi

I try to use @path from unittest.mock to mock (with return value) var inside FastApi route

route is:

from . import superlogic

@router.post('/{item_id}/submit'):
async def submit(item: str):
   var_i_need_to_mock = super_object.very_long_complicated_logic()
   return True // nevermid

pytest:

@patch('app.router.submit'):
def test_submit(test_client):
    var_i_need_to_mock = True
    test_client.post('/submit') // But var_i_need_to_mock doesn't overrides

I can patch my super_object instead of var_i_need_to_mock and it works as expected like below @patch('app.router.submit', mockobject)

What is the best practice directly override var_i_need_to_mock inside route and how to achieve this

Upvotes: 2

Views: 1150

Answers (1)

M.O.
M.O.

Reputation: 2986

In this case, I would try to patch the return value of super_object.very_long_complicated_logic, like so:

@patch('filename.super_object.very_long_complicated_logic', return_value=True):
def test_submit(test_client):
    test_client.post('/submit')

Upvotes: 1

Related Questions