当基本模板在不同的,不相关的视图/ href中包含参数时,如何对Django视图进行单元测试

时间:2018-12-02 15:19:25

标签: django unit-testing

例如,我要尝试测试其功能的观点有所不同,但我无法解决具有不同的带有参数的href的问题。例如,在通过基本模板应用于每个视图的导航栏中,我具有指向用户个人资料的链接。看起来像:url(r'^(?P<username>[\w.@+-]+)/$', user_profile, name='user-profile')。因此,这取决于获取用户名作为参数: <a class="dropdown-item" href="{% url 'user-profile' request.user.username %}>。我已将测试设置为:

self.client.force_login(user=self.test_user)
request = self.client.get(reverse('home'))
request.user = self.test_user

它在第二行失败,因此我认为它永远没有机会看到request.user。因为我不是直接在导航栏中调用视图,而是链接到视图,所以我如何才能在没有得到Reverse for 'user-profile' with arguments '('',)' not found

的情况下对此进行测试

0 个答案:

没有答案
相关问题