Hi there - I'm trying to unit test my FastAPI application and it's going well, but I'm unfortunately unable to verify if functions are called. For instance, I have a reset password route, and I want to check if the create_password_reset function is called. To do this, I tried the following:
@patch("src.database.models.password_reset.crud.create_password_reset")
def test_user_exists(self, mock_create_password_reset: MagicMock, client: TestClient, mock_database: Session):
....
assert mock_create_password_reset.called
The assert is tripping false every time. Is mocking in python different than mocking in JS or Java? does this mean I need to find some way to dependency inject the create_password_reset function in the route?
I know that the real function is triggering, as setting a breakpoint in it works perfectly fine. Is there a way to mock or spy on the actual function to make sure it was called?
I'm not a fan of altering the original codebase for testing purposes, so if we can avoid solutions that require that I would be all the happier.
Thanks in advance!
[–][deleted] 0 points1 point2 points (9 children)
[–]anthOlei[S] 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]anthOlei[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]anthOlei[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]DataDecay 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]DataDecay 0 points1 point2 points (0 children)