Example #1
0
 def test_saves_list_owner_if_user_logged_in(self):
     request = HttpRequest()
     request.user = User.objects.create(email='*****@*****.**')
     request.POST['text'] = '신규 작업 아이템'
     new_list(request)
     list_ = List.objects.first()
     self.assertEqual(list_.owner, request.user)
Example #2
0
 def test_list_owner_is_saved_if_user_is_authenticated(self):
     request = HttpRequest()
     request.user = User.objects.create(email="*****@*****.**")
     request.POST["text"] = "신규 작업 아이템"
     new_list(request)
     list_ = List.objects.first()
     self.assertEqual(list_.owner, request.user)
    def test_saves_form_with_owner_if_form_valid(self, mockNewListForm):
        mock_form = mockNewListForm.return_value
        mock_form.is_valid.return_value = True

        new_list(self.request)

        mock_form.save.assert_called_once_with(owner=self.request.user)
Example #4
0
    def test_does_not_save_if_form_invalid(self, mockNewListForm):
        mock_form = mockNewListForm.return_value
        mock_form.is_valid.return_value = False

        new_list(self.request)

        self.assertFalse(mock_form.save.called)
Example #5
0
 def test_list_owner_is_saved_if_user_is_authenticated(self):
     request = HttpRequest()
     request.user = User.objects.create(email='*****@*****.**')
     request.POST['text'] = 'new list item'
     new_list(request)
     list_ = List.objects.first()
     self.assertEqual(list_.owner, request.user)
    def test_mock_list_owner_is_saved_if_user_is_authenticated(self, mockList):
        # 为视图创建一个真实的 List 对象。List 对象必须真实,否则视图尝试保存 Item 对象时会遇到外键错误(表明这个测试只是部分隔离)
        mock_list = List.objects.create()
        mock_list.save = Mock()
        mockList.return_value = mock_list
        request = HttpRequest()
        request.user = User.objects.create()  # 给 requests 对象赋值一个真实的用户
        request.POST["text"] = "new list item"
        new_list(request)
        self.assertEqual(mock_list.owner, request.user)  # 现在可以声明断言,判断清单对象是否设定了 .owner 属性

        mock_list = List.objects.create()
        mock_list.save = Mock()
        mockList.return_value = mock_list
        request = HttpRequest()
        request.user = Mock()
        request.user.is_authenticated.return_value = True
        request.POST["text"] = "new list item"

        # 定义一个函数,在这个函数中就希望先发生的事件声明断言,即检查是否设定了清单的属主
        def check_owner_assigned():
            self.assertEqual(mock_list.owner, request.user)

        # 把这个检查函数赋值给后续事件的 side_effect 属性。当视图在驭件上调用 save 方法时,才会执行其中的断言。要保证在测试的目标函数调用前完成此次赋值
        mock_list.save.side_effect = check_owner_assigned
        new_list(request)
        # 最后,要确保设定了 side_effect 属性的函数一定会被调用,也就是要调用 .save() 方法。否则断言永远不会运行
        mock_list.save.assert_called_once_with()
 def test_saves_list_owner_if_user_logged_in(self):
     request = HttpRequest()
     request.user = User.objects.create(email="*****@*****.**")
     request.POST["text"] = "new list item"
     new_list(request)
     list_ = List.objects.first()
     self.assertEqual(list_.owner, request.user)
 def test_POST_from_real_user_sets_owner_on_list(self):
     user = User.objects.create(email='*****@*****.**')
     request = HttpRequest()
     request.user = user
     request.POST['text'] = 'new item'
     new_list(request)
     list_ = List.objects.all().get()
     self.assertEqual(list_.owner, user)
Example #9
0
    def test_saves_form_with_owner_if_form_valid(self, mockNewListForm):
        mock_form = mockNewListForm.return_value
        # has an is_valid function
        mock_form.is_valid.return_value = True
        new_list(self.request)

        # a save method that deals with a user
        mock_form.save.assert_called_once_with(owner=self.request.user)
Example #10
0
	def test_list_owner_is_saved_if_user_is_authentivated(self,mockList):
		mock_list = List.objects.create()
		mock_list.save = Mock()
		mockList.return_value = mock_list
		request = HttpRequest()
		request.user = User.objects.create()
		request.POST['text']='new list item'

		new_list(request)

		self.assertEqual(mock_list.owner,request.user)
Example #11
0
    def test_redirects_to_form_returned_object_if_form_valid(self, mock_redirect, mockNewListForm):
        mock_form = mockNewListForm.return_value
        mock_form.is_valid.return_value = True
        response = new_list(self.request)

        self.assertEqual(response, mock_redirect.return_value)
        mock_redirect.assert_called_once_with(mock_form.save.return_value)
    def test_renders_home_template_with_form_if_form_invalid(self, mock_render, mockNewListForm):
        mock_form = mockNewListForm.return_value
        mock_form.is_valid.return_value = False

        response = new_list(self.request)
        self.assertEqual(response, mock_render.return_value)
        mock_render.assert_called_once_with(self.request, "home.html", {"form": mock_form})
    def test_redirects_to_form_returned_object_if_form_valid(self, mock_redirect, mockNewListForm):
        mock_form = mockNewListForm.return_value
        mock_form.is_valid.return_value = True  # 指定测试的是表单中数据有效的情况

        response = new_list(self.request)

        self.assertEqual(response, mock_redirect.return_value)  # 检查视图的响应是否为 redirect 函数的结果
        # 然后检查调用 redirect 函数时传入的参数是否为在表单上调用 save 方法得到的对象
        # 模拟的 form.save 方法返回一个对象,我们希望在视图中使用这个对象。
        mock_redirect.assert_called_once_with(mock_form.save.return_value)
Example #14
0
 def test_new_list(self):
     request=HttpRequest()
     request.method='POST'
     request.POST['item_text']='Interestring'
     response=new_list(request)
     request=HttpRequest()
     request.method='POST'
     request.POST['item_text']='Interestring2'
     response=add_item(request,1)
     response=self.client.get('/lists/%d/' % (1,))
Example #15
0
 def test_redirects_to_form_returned_object_if_form_valid(
     self, mock_redirect, mockNewListForm
 ):
     mock_form = mockNewListForm.return_value
     ## We specify we're testing the case where the form is valid
     mock_form.is_valid.return_value = True
     response = new_list(self.request)
     ## Check that the response from the view is the result of the 
     ## redirect function
     self.assertEqual(response, mock_redirect.return_value)
     ## Check that the redirect function was called with the object that
     ## the form returns on save
     mock_redirect.assert_called_once_with(mock_form.save.return_value)
Example #16
0
 def test_passes_POST_data_to_NewListForm(self, mockNewListForm):
     new_list(self.request)
     # initialize form with post request data
     mockNewListForm.assert_called_once_with(data=self.request.POST)
 def test_passes_POST_data_to_NewListForm(self, mockNewListForm):
     new_list(self.request)
     mockNewListForm.assert_called_once_with(data=self.request.POST)
Example #18
0
 def check_owner_assigned():
     self.assertEqual(mock_list.owner, request.user)
     mock_list.save.side_effect = check_owner_assigned
     new_list(request)
     mock_list.save.assert_called_once_with()
 def test_passes_POST_data_to_NewListForm(self, mockNewListForm):
     new_list(self.request)
     # 然后检查视图要做的第一件事:在视图中使用正确的构造方法初始化它的协作者,即 NewListForm,传入的数据从请求中读取
     mockNewListForm.assert_called_once_with(data=self.request.POST)
Example #20
0
	def test_save_not_called_if_form_is_invalid(self, mockNewListForm):
		mock_form = mockNewListForm.return_value
		mock_form.is_valid.return_value = False
		new_list(self.request)
		self.assertFalse(mock_form.save.called,
						 self.message.format(times=mockNewListForm.call_count))