I am using unittest
in my project, and the following is my test code.
import unittest
from unittest.mock import patch, Mock
from lists.views import NewListView
@patch('lists.views.NewListForm')
class NewListViewUnitTest(unittest.TestCase):
def setUp(self):
self.request = HttpRequest()
self.request.POST['text'] = 'new list item'
self.request.user = Mock()
def test_passes_POST_data_to_NewListForm(self, mockNewListForm):
NewListView(self.request) # The important bit
mockNewListForm.assert_called_once_with(data=self.request.POST)
I want to pass a custom request, which as you see has some POST data. I had originally had a view, so I could pass a custom request. Now, with a class based view, how can I do that? I am currently getting an error which says:
TypeError: __init__() takes 1 positional argument but 2 were given
I did try NewListView.as_view(self.request)
, but that says
TypeError: as_view() takes 1 positional argument but 2 were given
I know you should use the Django test client and make POST requests using the URL, not calling the actual view, but this is how they did it in the book I am currently studying (Test-Driven Development with Python by Harry Percival. Unfortunately, he doesn't seem to have updated his appendices, where he teaches the class based view stuff, so I am doing it my self. Or trying to.)
Thanks.
Aucun commentaire:
Enregistrer un commentaire