angularjs – 业力中服务呼叫成功响应的代码覆盖率
发布时间:2020-12-17 17:10:48 所属栏目:安全 来源:网络整理
导读:我有这样的功能 $scope.openMail=function(mail){ DocumentTypes.getDocument(function(response){ $scope.documentTypes=response.result; $log.log("documentTypes",$scope.documentTypes); })} 以上乐趣的规格是 it("should test open mail",function(){
我有这样的功能
$scope.openMail=function(mail){ DocumentTypes.getDocument(function(response){ $scope.documentTypes=response.result; $log.log("documentTypes",$scope.documentTypes); }) } 以上乐趣的规格是 it("should test open mail",function(){ scope.documentTypes=[{"type":"pdf"},{"type":"xml"}]; spyOn(documentTypes,"getDocument").and.callFake(function(){ return scope.documentTypes; }); var mail='back'; scope.openMail(mail); expect(scope.documentTypes).toEqual({"type":"pdf"},{"type":"xml"}); }) 所以代码没有涵盖功能(响应){} 如何在代码覆盖中涵盖此代码?谢谢. 解决方法
您的测试有几个问题:
>你做spyOn(documentTypes,“getDocument”)而不是spyOn(DocumentTypes,“getDocument”) 这是我将如何测试它: describe('$scope.openMail',function() { beforeEach(function() { spyOn(DocumentTypes,'getDocument'); }); it('uses DocumentTypes.getDocument service to get the document types',function() { $scope.openMail('test_mail'); expect(DocumentTypes.getDocument).toHaveBeenCalledWith(jasmine.any(Function)); }); describe('provides a callback function that',function() { beforeEach(function() { DocumentTypes.getDocument.and.callFake(function (callback) { callback('test_document_types'); }); }); it('stores the document types on the scope',function() { $scope.openMail('test_mail'); expect($scope.documentTypes).toEqual('test_document_types'); }); // Note: This is optional,depending on whether you test logging or not it('logs the document types',function() { spyOn($log,'log'); $scope.openMail('test_mail'); expect($log.log).toHaveBeenCalledWith('documentTypes','test_document_types'); }); }); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |