title: How to write a test for a custom action on a viewset in Django Rest Framework
body: <p>I am new to Django, and Django Rest Framework.</p>
  <p>I would like to know how to go about testing custom actions. For example, assume we have the following code from the DRF tutorials</p>
  <pre><code>class UserViewSet(viewsets.ModelViewSet):
      &quot;&quot;&quot;
      A viewset that provides the standard actions
      &quot;&quot;&quot;
      queryset = User.objects.all()
      serializer_class = UserSerializer
  
      @action(detail=True, methods=['post', 'put'])
      def set_password(self, request, pk=None):
          user = self.get_object()
          serializer = PasswordSerializer(data=request.data)
          if serializer.is_valid():
              user.set_password(serializer.data['password'])
              user.save()
              return Response({'status': 'password set'})
          else:
              return Response(serializer.errors,
                              status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  <p>How would I go about calling this view in a test in DRF.</p>
  <pre><code>def test_password_set(self):
      user = User.objects.create(name=&quot;Joe&quot;)
      factory = APIRequestFactory()
      request_url = f'users/{user.id}/set_password/'
      request = factory.post(request_url)
  
      view = UserViewSet.as_view({'put': 'update'})
      response = view(request)
      self.assertEqual(response.status_code, 200)
  
  </code></pre>
  <p>That code gives me the error below</p>
  <pre><code>AssertionError: 405 != 200
  </code></pre>
  <p>which means that the given method is not allowed.</p>
  <p>Could anyone help me figure out what the error could be?</p>