How to control the valid return of mock object?

Viewed 24
def create(user):
    user.password = hash_function(user.password)
    db.add(user)


@patch('hash_function')
def test_create_user(hash_function):
    create(User())
    hash_function.assert_called()

When I only need to assert_called with hash_function. I don't care about the return value, just want to know this function has called.

So I use hash_function.assert_called() in the last of test function.

But the code is break because: db.add(user) don't allow to push the user with the password is a mock object.

The reason "password is a mock object" is hash_function already a mock object.

So I wonder, is this safe to write test code with @patch, because it make your object become a mock, then when another function use the return value of mock(). It's gonna break the code.

0 Answers
Related