加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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"});


  })

所以代码没有涵盖功能(响应){}

so the code is not covering code for <code>function(response){}</code>


如何在代码覆盖中涵盖此代码?谢谢.

解决方法

您的测试有几个问题:

>你做spyOn(documentTypes,“getDocument”)而不是spyOn(DocumentTypes,“getDocument”)
>你的假函数返回一个值(同步)而不是调用提供的回调(异步)
>首先将scope.documentTypes初始化为测试的预期结果,即无论代码做什么,测试都会通过(除非你得到例外)
>更多代码问题 – 您正在测试的函数对输入邮件参数没有任何作用

这是我将如何测试它:

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');
        });
    });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读