请帮助纠正测试。
我需要测试网址。
urls.py:
urlpatterns = patterns('app_zapsum',
url(r'^$', 'views.rules', name='rules'),
)
views.py:
def rules(request):
t = loader.get_template('page_rules.html')
c = RequestContext(request, {})
return HttpResponse(t.render(c))
tests.py:
from django.test import TestCase, RequestFactory
from app_zapsum.views import rules
from django.core.urlresolvers import reverse
from django.core.urlresolvers import resolve
class TestUrls(TestCase):
def test_details(self):
resolver = resolve('/')
self.assertEqual(resolver.rules, rules)
由于命令python manage.py test显示以下错误消息:
======================================================================
ERROR: test_details (app_zapsum.tests.TestUrls)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/kalinins/.virtualenvs/zapsum/zapsum/app_zapsum/tests.py", line 47, in test_details
self.assertEqual(resolver.rules, rules)
AttributeError: 'ResolverMatch' object has no attribute 'rules'
----------------------------------------------------------------------
Ran 3 tests in 0.068s
FAILED (errors=1)
Destroying test database for alias 'default'...
答案 0 :(得分:1)
urlresolvers.resolve
返回urlresolvers.ResolverMatch
个对象。该对象具有func
对象:
class TestUrls(TestCase):
def test_details(self):
resolver = resolve('/')
self.assertEqual(resolver.func, rules)