W3C 通知覆盖范围

W3C Notification Coverage

本文关键字:范围 覆盖 通知 W3C      更新时间:2023-09-26

由于 W3C 通知不适用于 PhantomJS,我无法将代码覆盖率提高到 100%。我有以下功能:

function requirespermission(overwrite){
    if(overwrite || (typeof Notification !== 'undefined' && Notification.permission === 'granted'))
    {
        return true; 
    }
    else if(!overwrite || typeof Notification !== 'undefined'){
        Notification.requestPermission();
    }
    return false;
}

我的测试如下:

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 作为单元测试浏览器
  • 科伯图拉 饰 记者
  • (詹金斯 饰 CI)

如何让 else 函数通过代码覆盖率测试?

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

function requirespermission(overwrite){
    if(!Notify.needsPermission || overwrite)
    {
        return true; 
    }
    else if(Notify.isSupported()){  
        Notify.requestPermission();
    }
    return false;
}

和测试:

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