我一直试图找出我在django 1.8.3中创建的代码的内容......当我使用django的测试运行器时(更具体地说是命令:{ {1}})我总是收到相同的错误消息:
文件" C:\ Users \ dimitris \ djangop \ oursite \ predsite \ tests.py",第117行,在test_long_list中 self.assertTrue('分页'在response.content中) AssertionError:False不正确
以下是我详细创建的代码:
的 OURSITE(项目)>> PREDSITE(APP)>> MODELS.PY:
python manage.py test predsite
OURSITE>> PREDSITE>> VIEWS.PY :
from django.db import models
from django.contrib.auth.models import User
import os
def hashed_uploads_dirs(instance, filename):
"""Returns path with md5 hash as directory"""
return os.path.join(instance.md5, filename)
class Datafile(models.Model):
file_id = models.IntegerField(default=0, primary_key=True)
datafile = models.FileField(upload_to='.')
user = models.ForeignKey(User, default=1)
md5 = models.CharField(max_length=32)
OURSITE>> OURSITE>> URLS.PY
class FileListView(ListView):
model = Datafile
queryset = Datafile.objects.order_by('-file_id')
context_object_name = "files"
template_name = "index.html"
paginate_by = 5
class FileAddView(FormView):
form_class = DatafileForm
success_url = reverse_lazy('home')
template_name = "add.html"
def form_valid(self, form):
form.save(commit=True)
messages.success(self.request, 'File uploaded!', fail_silently=True)
return super(FileAddView, self).form_valid(form)
class FileAddHashedView(FormView):
"""This view hashes the file contents using md5"""
form_class = DatafileForm
success_url = reverse_lazy('home')
template_name = "add.html"
def form_valid(self, form):
hash_value = hashlib.md5(form.files.get('datafile').read()).hexdigest()
# form.save returns a new Datafile as instance
instance = form.save(commit=False)
instance.md5 = hash_value
instance.save()
messages.success(
self.request, 'File hashed and uploaded!', fail_silently=True)
return super(FileAddHashedView, self).form_valid(form)
OURSITE>> PREDSITE>> CONTEXT_PROCESSORS.PY
urlpatterns = patterns('',
url(r'^add/$', FileAddView.as_view(), name='datafile-add'),
url(r'^add/files/$', FileListView.as_view(), name='home'),
) + static(settings.MEDIA_URL, datafile_root=settings.MEDIA_ROOT)
OURSITE>> PREDSITE>> FORMS.PY
from django.conf import settings
def static_root(request):
"""
Adds static-root context variables to the context.
"""
return {'STATIC_ROOT': settings.STATIC_ROOT}
def media_root(request):
"""
Adds media-root context variables to the context.
"""
return {'MEDIA_ROOT': settings.MEDIA_ROOT}
OURSITE>> PREDSITE>> TESTS.PY (我提供错误发生的部分)
class DatafileForm(forms.ModelForm):
"""Upload files with this form"""
class Meta:
model = Datafile
exclude = ('md5',)
现在重要的模板如下: 的 OURSITE>> TEMPLATES>> index.html的
def test_long_list(self):
"""Get a paginated home page"""
filename = "./file_{}.txt"
for x in xrange(10):
fb = Datafile(datafile=filename.format(x))
fb.save()
response = self.client.get(reverse('home'))
self.assertEqual(response.status_code, 200)
self.assertTrue('pagination' in response.content)
self.assertEqual(
len(response.context['files']),
FileListView.paginate_by)

因为很明显有一个名为" pagination"在上面的模板中,我怎么可能在执行测试运行器时收到此错误消息?
非常感谢,提前!!
答案 0 :(得分:2)
这是因为div data-win-control="WinJS.UI.ListView" data-win-options="{
itemDataSource : Episodes.Data.itemsBindingList.dataSource
}"></div>
的{{1}}属性返回了上下文变量,不是 "{\"exception\":null,\"error\":{\"description\":\"It's not possible to obtain the property 'dataSource' of undifined or null reference\"
代码。相反,如果这是您的目标,您可以测试context
列表中您要查找的项目。
请尝试使用assertContains。文档中的语法表明您可能希望将以下内容添加到测试中:
response