Testing ModelSerializers
<p>I am trying to learn the best way to test my serializers but having some issues. In the below example I am testing to see if creating new users works correctly. ie can hash the password. The error i am getting is:</p>

<blockquote>
  <p>TypeError: create() missing 1 required positional argument:
  'validated_data'</p>
</blockquote>

<p>Im not really sure what I need to pass in as <code>validated_data</code> </p>

<p>serializers.py</p>

<pre><code>class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'password')
        extra_kwargs = {
            'url': {'view_name': 'user-detail'},
            'password': {'write_only': True}
        }

    def create(self, validated_data):
        user = User.objects.create_user(
            username=validated_data['username'],
            email=validated_data['email'])

        user.set_password(validated_data['password'])
        user.save()

        return user

    def update(self, instance, validated_data):
        user = User.objects.get(id=instance.id)
        user.set_password(validated_data['password'])
        user.save()

        return user
</code></pre>

<p>tests.py</p>

<pre><code>class UserSerializerTest(APITestCase):
    def setUp(self):

        factory = APIRequestFactory()

        request = factory.get(path=reverse('user-list'))

        self.serializer_context = {
            'request': Request(request),
        }

    def test_create_user(self):
        from .serializers import UserSerializer

        data = {'username': 'temp_usr', 'email': 'temp@email.com',
                'password': 'temp_pass'}

        user = UserSerializer(data=data, context=self.serializer_context).create()
</code></pre>

<p>You called <code>create</code> in your test. You should call <code>is_valid</code> and then <code>save</code>.</p>

<pre><code>user = UserSerializer(data=data, context=self.serializer_context)
user.is_valid(raise_exception=True)
user.save()
</code></pre>

python django django-rest-framework