[Answer]-Django / Python unittest side_effect usage

1👍

Just make a separate function that raises FieldError if it’s called one way, and doesn’t in the other:

expected_return = MagicMock()

def fake_filter(**kwargs):
    if 'tags__name__in' in kwargs:
        raise FieldError()
    if 'tags__icontains' in kwargs:
        return expected_return
    raise ValueError("Neither expected kwarg present")

mock_queryset = MagicMock()
mock_queryset.filter = fake_filter

et cetera. I see now that you already do that with your side effect function, you can just do the same but more.

Leave a comment