# my .views
def post_create_view(request):
form = PostModelForm(request.POST or None)
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.save()
form = PostModelForm()
context = {'form': form}
return render(request, 'form.html', context)
# my .urls
path('create/', post_create_view, name='create')
# my .test
class TestViews(TestCase):
def setUp(self):
self.client = Client()
self.create_url = reverse('posts:create')
self.user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
self.post1 = Post.objects.create(user=self.user, title='testpost', content='fgh')
self.query = Post.objects.all()
def test_blog_create_POST(self):
# this is where the error surely is
response = self.client.post(self.create_url, {'title': 'testpost2', 'content': 'asd'})
self.assertEqual(response.status_code, 302)
# this is where the test fail
self.assertEqual(self.query.last().title, "testpost2")
'testpost2' doesn't get created but my form works fine. How can I simulate the inputs in my testcase? self.create_url redirect to the correct url, the one with the form in the template, such form works great, but when I write the response in my test the dictionary I pass as second arguments seems not to go through.
Aucun commentaire:
Enregistrer un commentaire