python - TypeError:__init__() got an unexpected keyword argument 'entry' -
i'm trying complete tutorial http://test-driven-django-development.readthedocs.io/en/latest/06-the-testing-game.html following steps beginning of tutorial. main issue init() method , unexpected keyword 'entry'
/tests.py
# -*- coding: utf-8 -*- __future__ import unicode_literals django.contrib.auth import get_user_model django.test import testcase .forms import commentform .models import entry, comment django_webtest import webtest # create tests here. class entrymodeltest(testcase): '''def setup(self): self.user = get_user_model().objects.create(username='some_user') ''' def test_string_representation(self): entry = entry(title="my entry title") self.assertequal(str(entry), entry.title) def test_verbose_name_plural(self): self.assertequal(str(entry._meta.verbose_name_plural), "entrys") def test_homepage(self): response = self.client.get('/') self.assertequal(response.status_code, 200) def test_get_absolute_url(self): user = get_user_model().objects.create(username='some_user') entry = entry.objects.create(title="my entry title", author=user) self.assertisnotnone(entry.get_absolute_url()) class homepagetests(testcase): """test whether our blog entries show on homepage""" def setup(self): self.user = get_user_model().objects.create(username='some_user') def test_one_entry(self): entry.objects.create(title='1-title', body='1-body', author=self.user) response = self.client.get('/') self.assertcontains(response, '1-title') self.assertcontains(response, '1-body') def test_two_entries(self): entry.objects.create(title='1-title', body='1-body', author=self.user) entry.objects.create(title='2-title', body='2-body', author=self.user) response = self.client.get('/') self.assertcontains(response, '1-title') self.assertcontains(response, '1-body') self.assertcontains(response, '2-title') def test_no_entries(self): response = self.client.get('/') self.assertcontains(response, 'no blog entries yet.') class entryviewtest(webtest): def setup(self): self.user = get_user_model().objects.create(username='some_user') self.entry = entry.objects.create(title='1-title', body='1-body', author=self.user) def test_basic_view(self): response = self.client.get(self.entry.get_absolute_url()) self.assertequal(response.status_code, 200) def test_title_in_entry(self): response = self.client.get(self.entry.get_absolute_url()) self.assertcontains(response, self.entry.title) def test_body_in_entry(self): response = self.client.get(self.entry.get_absolute_url()) self.assertcontains(response, self.entry.body) def test_view_page(self): page = self.app.get(self.entry.get_absolute_url()) self.assertequal(len(page.forms), 1) class commentmodetest(testcase): def setup(self): self.user = get_user_model().objects.create(username='some_user') self.entry = entry.objects.create(title='1-title', body='1-body', author=self.user) def test_string_representation(self): comment = comment(body="my comment body") self.assertequal(str(comment), comment.body) def test_no_comment(self): response = self.client.get(self.entry.get_absolute_url()) self.assertcontains(response, 'no comments yet.') class commentformtest(testcase): def setup(self): self.user = get_user_model().objects.create_user('zoidberg') self.entry = entry.objects.create(author=self.user, title="my entry title") def test_init(self): commentform(entry=self.entry) def test_init_without_entry(self): self.assertraises(keyerror): commentform() def test_valid_data(self): form = commentform({ 'name': "turanga leela", 'email': "testing@unomena.com", 'body': "hi there", }, entry=self.entry).asserttrue(form.is_valid()) comment = form.save() self.assertequal(comment.name, "turanga leela") self.assertequal(comment.email, "testing@unomena.com") self.assertequal(comment.body, "hi there") self.assertequal(comment.entry, self.entry) def test_blank_data(self): form = commentform({}, entry=self.entry) self.assertfalse(form.is_valid()) self.assertequal(form.errors, { 'name': ['this field required.'], 'email': ['this field required.'], 'body': ['this field required.'], })
/forms.py
from django import forms .models import comment #from .models import entry class commentform(forms.modelform): class meta: model = comment fields = ('name', 'email', 'body') def __init__(self, *args, **kwargs): self.entry = kwargs.pop('entry') super(commentform, self).__init__(*args, **kwargs) def save(self): comment = super(commentform, self).save(commit=false) comment.entry = self.entry comment.save() return comment
command screenshot: https://gyazo.com/47a6801ab6dc2b525c320ad2880ec9ea
Comments
Post a Comment