首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >W3C通知覆盖范围

W3C通知覆盖范围
EN

Stack Overflow用户
提问于 2016-03-08 12:14:37
回答 1查看 43关注 0票数 1

由于W3C通知不适用于PhantomJS,我很难将代码覆盖率提高到100%。我的职能如下:

代码语言:javascript
复制
function requirespermission(overwrite){
    if(overwrite || (typeof Notification !== 'undefined' && Notification.permission === 'granted'))
    {
        return true; 
    }
    else if(!overwrite || typeof Notification !== 'undefined'){
        Notification.requestPermission();
    }
    return false;
}

我的测试如下:

代码语言:javascript
复制
it('should be able to get permission', function(){
    notificationservice.requirespermission(true);
});

it('should be able to not get permission', function(){
    notificationservice.requirespermission(false);
});

但是不管我做什么,else函数的条件覆盖率保持在2/4 (这很有道理,但我必须实现对跨浏览器支持的检查)。我使用以下工具:

  • 卡玛+茉莉花考试
  • 业力-覆盖范围
  • PhantomJS作为单元测试浏览器
  • 科伯图拉担任记者
  • (Jenkins为CI)

如何通过代码覆盖率测试才能获得tests函数?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-08 14:18:41

我通过使用Notify.js对通知使用包装器来解决这个问题。现在的代码是:

代码语言:javascript
复制
function requirespermission(overwrite){
    if(!Notify.needsPermission || overwrite)
    {
        return true; 
    }
    else if(Notify.isSupported()){  
        Notify.requestPermission();
    }
    return false;
}

而这些测试:

代码语言:javascript
复制
it('should be able to not get permission', function(){
    notificationservice.requirespermission(true);
});

it('should be able to not get permission', function(){
    spyOn(Notify, 'isSupported').and.returnValue(true);
    notificationservice.requirespermission(false);
});

it('should handle not-supported', function(){
    spyOn(Notify, 'isSupported').and.returnValue(false);
    notificationservice.requirespermission(false);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35867128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档