我正在尝试为一个类编写单元测试,该类有一个POST方法,用于将文档上载到基于web的django应用程序。下面是我要为其编写单元测试的视图类:
class SOP(APIView):
authentication_classes = (authentication.TokenAuthentication,)
def post(self,request):
returnDict={}
returnDict['msg']='File not uploaded'
#if form.is_valid():
newdoc = Document(sopFile = request.FILES['sopFile'])
newdoc.save()
returnDict['msg']='File uploaded'
returnDict['fileName']=newdoc.sopFile.name
# Redirect to the document list after POST
return Response(returnDict)
我的django应用程序正在使用表单.py为了上传一个文件,所以我把代码也和这个放在一起:
^{pr2}$我尝试过使用RequestFactory()和TestCase()来编写测试用例,但是我不知道如何为这种类型的类/视图编写单元测试。。。在
你可以尝试用this这样的东西来嘲笑你所需要的东西。在
您可以使用Django中的the test client。它很容易使用。在
Django文档中的示例:
相关问题 更多 >
编程相关推荐