I have a simple django form like this:
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
My view uses it like this:
def my_view(request):
form = ContactForm(request.POST)
if form.is_valid():
data = form.cleaned_data
...
I want to test my view and don't care about what the form actually does. This is what my test looks like so far
@patch.object(ContactForm, 'is_valid')
def test_my_view(mock_is_valid):
is_valid.return_value = True
...
assert response.status_code == 201
However, this doesn't work because form.cleaned_data
is not set until form.is_valid()
is called. How do I mock out the form.cleaned_data
attribute if it doesn't exist in the first place?