我在views.py
中运行了这个方法的测试用例views.py
69 def delete(request, activityId):
70 """Delete activity form database
71 Return to templates if the owner does not match the user
72 otherwise, return to actuser:activity
73 """
74
75 activity = ActivityViewModel()
76 activity.get_by_id(activityId)
77 userName = activity.db.owner
78
79 deleteThisActivity = activity.delete_from_actinbox(activity.db.owner, request.user.username)
80
81 if deleteThisActivity == True:
82 return HttpResponseRedirect(reverse('actuser:activity', args=(userName,)))
83 else:
84 activityForm = ActivityForm(instance=activity.db)
85 return render(request, 'activity/profile.html', {
86 'message': "You Are Not Allowed To Delete This Activity",
87 'activity': activity.db,
88 'form': activityForm,
89 })
这是我的测试,但我不确定它是如何工作的。
tests.py
def test_activity_delete(self):
"""test activity that delete from database
"""
delete_activity = create_activity(name="New Activity", userName="esutek")
response = self.client.get(reverse('activity:delete', args=(delete_activity.pk,)))
self.assertEqual(0, delete_activity)
我非常感谢你的帮助。
答案 0 :(得分:0)
使用以下方法进行测试时会发生什么:
self.assertIsInstance(response, HttpResponseRedirect)
作为你的断言?
此外,您还需要检查活动是否已被删除:
val = Activity.get(delete_activity.pk)
self.assertEqual(val, None)
答案 1 :(得分:0)
试着看看这件事何时会破裂;然后编写适当的断言语句。
让我们看第二个案例;在这种情况下,测试的输入是:
activity_id
,以及与活动不匹配的用户。样本测试可能是:
def test_delete_with_invalid_user_for_activity(self):
c = self.client.login(username='another_user', password='sekret')
u = User.objects.get(username='another_user')
activity = Activity.objects.exclude(db.owner=u)[0]
response = c.get(reverse('activity:delete', args=(activity.pk,)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['message'],
'You Are Not Allowed To Delete This Activity')
c.logout()