[Answered ]-Django – TDD: 'HttpRequest' has no attribute 'POST'

1👍

You forgot the parentheses after HttpRequest 🙂

That’s why django is saying HttpRequest has no attribute 'POST'

1👍

You should consider using RequestFactory for your test cases.

Example from the docs:

from django.contrib.auth.models import AnonymousUser, User
from django.test import TestCase, RequestFactory

from .views import MyView, my_view

class SimpleTest(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()
        self.user = User.objects.create_user(
            username='jacob', email='jacob@…', password='top_secret')

    def test_details(self):
        # Create an instance of a GET request.
        request = self.factory.get('/customer/details')

        # Recall that middleware are not supported. You can simulate a
        # logged-in user by setting request.user manually.
        request.user = self.user

        # Or you can simulate an anonymous user by setting request.user to
        # an AnonymousUser instance.
        request.user = AnonymousUser()

        # Test my_view() as if it were deployed at /customer/details
        response = my_view(request)
        # Use this syntax for class-based views.
        response = MyView.as_view()(request)
        self.assertEqual(response.status_code, 200)
👤Goran

0👍

When testing, if you need a request object, you can also use the request factory to generate one

Reference from the docs:
https://docs.djangoproject.com/en/1.11/topics/testing/advanced/

from django.contrib.auth.models import AnonymousUser, User
from django.test import TestCase, RequestFactory

from .views import MyView, my_view

class SimpleTest(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()
        self.user = User.objects.create_user(
            username='jacob', email='jacob@…', password='top_secret')

    def test_details(self):
        # Create an instance of a GET request.
        request = self.factory.get('/customer/details')

        # Recall that middleware are not supported. You can simulate a
        # logged-in user by setting request.user manually.
        request.user = self.user

        # Or you can simulate an anonymous user by setting request.user to
        # an AnonymousUser instance.
        request.user = AnonymousUser()

        # Test my_view() as if it were deployed at /customer/details
        response = my_view(request)
        # Use this syntax for class-based views.
        response = MyView.as_view()(request)
        self.assertEqual(response.status_code, 200)

In your case, your test could look like this:

from django.test import RequestFactory

...

def test_home_page_can_save_POST_request(self):
    factory = RequestFactory()
    response = factory.post('PATH_TO_YOUR_VIEW', data={
        'item_text': 'A new list item',
    })
    self.assertIn('A new list item', response.content.decode())

0👍

I have two solution.

1.- Create request object with RequestFactory and calling at views.

def test_home_page_can_save_a_POST_request(self):
    request = self.factory.post("/", data={'item_text': 'A new list item',})
    response = home_page(request)
    self.assertIn('A new list item', response.content.decode())

2.- Or use the response of RequestFactory().

  def test_home_page_can_save_a_POST_request(self):
        response = self.factory.post("/", data={'item_text': 'A new list item',})
        self.assertIn('A new list item', response.readlines()[3].decode())

Leave a comment