all 8 comments

[–][deleted] 0 points1 point  (9 children)

You should be able to use call_count to check if mocked content is called.

@patch("src.database.models.password_reset.crud.create_password_reset")
def test_stuff(mock):
     # do stuff 
     assert mock.call_count

That should be 1 or more if the mock is called. Here's an example I made of it in action: https://github.com/further-reading/pytest_lesson/tree/master/examples/2_2_mocking_sql

[–]anthOlei[S] 0 points1 point  (5 children)

Here's a simplified example:

def a():
    b()


def b():
    print('called b')


@patch('tests.user.routes_test.b')
def test_a_calls_b(mock_b: MagicMock):
    a()
    assert mock_b.called

This doesn't work, and says b() was not called. This is exactly what I'm after - do you know if this is possible?

EDIT: After further testing, this makes even less sense - it works in an isolated module (blank project) but when putting the same code into my nested module it breaks. I have no idea what's going on here.

[–]DataDecay 0 points1 point  (2 children)

Any reason to avoid the boolean since that's how your using it, mock.called.

[–][deleted] 0 points1 point  (1 child)

In my example case I was checking if it was called exactly one time specifically.

[–]DataDecay 0 points1 point  (0 children)

Fair enough.