[Answered ]-Test django post with factory_boy

2👍

<User> is a django model and doesn’t know how to represent itself as JSON. You need to use a serializer like ModelSerializer from rest-framework or the django builtin serializing capabilities:
tests.py

from django.core import serializers
data = serializers.serialize('json', my_factory.stub(), fields=('id'))

a = self.client.post(
    my_url,data,
    content_type="application/json")
assert a.status_code == 403

Please note that any test using the django test client would be considered an integration test by most.

This does not mean that it is a bad test, I have many tests just like your’s, however they are integration tests.

If you would like to make this more of a unit-test try setup_view from here.

Leave a comment