由于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 (这很有道理,但我必须实现对跨浏览器支持的检查)。我使用以下工具:
如何通过代码覆盖率测试才能获得tests函数?
发布于 2016-03-08 14:18:41
我通过使用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);
});https://stackoverflow.com/questions/35867128
复制相似问题