我正在关注本教程。test_views.py根据作者的说法,我在运行时遇到了一个错误,该错误不应该存在TypeError: quote_from_bytes() expected bytes。Myviews和mytest_views就像本书一样,但是我使用的是django 2.0.6而不是django 1.11,所以我进行了url.py更改,所以也许是问题所在。编辑:乍一看,问题似乎出在mock()函数中。当我使用patch('lists.views.List')了Print(list_)我的观点给出<MagicMock name='List()' id='79765800'>的,而不是List object (1)/编辑我的lists/urls.py:urlpatterns = [ path('new', views.new_list, name='new_list'), path('<slug:list_id>/', views.view_list, name='view_list'), path('users/<email>/', # I'm not sure about this one but it works in other tests views.my_lists, name='my_lists'),]#instead of:#urlpatterns = [# url(r'^new$', views.new_list, name='new_list'),# url(r'^(\d+)/$', views.view_list, name='view_list'),# url(r'^users/(.+)/$', views.my_lists, name='my_lists'),#]我的lists/views.py:[...]def new_list(request): form = ItemForm(data=request.POST) if form.is_valid(): list_ = List() list_.owner = request.user list_.save() form.save(for_list=list_) Print(list_) return redirect(list_) else: return render(request, 'home.html', {"form": form})我的lists/tests/test_views.py:@patch('lists.views.List')@patch('lists.views.ItemForm')def test_list_owner_is_saved_if_user_is_authenticated(self, mockItemFormClass, mockListClass): user = User.objects.create(email='a@b.com') self.client.force_login(user) self.client.post('/lists/new', data={'text': 'new item'}) mock_list = mockListClass.return_value self.assertEqual(mock_list.owner, user)可以是什么?
添加回答
举报
0/150
提交
取消