我有一个基于阶级的观点
class HomePage(View):
def get(self, request):
return HttpResponse('<p>This is content.</p>')
和 url-pattern 定义如下:
urlpatterns = patterns('',
url(r'^$', HomePage.as_view()),
)
为了将此模式解析为当前视图函数,我编写了一个如下测试:
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
self.assertIsInstance(found.func, HomePage)
通过运行此单元测试,我收到以下错误:
self.assertIsInstance(found.func, HomePage)
AssertionError: <function HomePage at 0x7f85dd2c7840> is not an instance of <class 'web.views.HomePage'>
知道如何测试这个案例吗?
姜戈的View.as_view()
创建一个函数view_class
指向基于类的视图的属性。所以使用:
self.assertEquals(found.func.view_class, HomePage)
避免了不同模块中两个基于类的视图具有相同名称的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)