Hi Devs,
I was implementing unit-tests in my Django project and stumbled upon the following issue.
I want to unit-test my POST route. I do not want to use the test client already shipped with Django (using it in my e2e tests). I want to know how do I prepare my request object to pass to my view. Here is what I have done currently.
test_views.py
class CreateBlogTest(BaseViewTest):
@classmethod
def setUpClass(cls):
cls.request.method = "POST"
def test_create_valid_blog(self):
self.request.content_type = "application/json"
self.request._body = json.dumps({"title": "new title", "body": "new body"})
response = views.blog_collection(self.request)
self.assertEqual(response.status_code, 201)
In my view, I am accessing the data through request.data and passing it to a serializer.
In my current setting, I am getting a 400 error message when I have checked that the user does not exist.
Any suggestions regarding the same?
Thanks,
onlinejudge95