如何模拟$ window.Notification

时间:2016-09-19 10:47:26

标签: angularjs unit-testing jasmine karma-jasmine

在使用角度进行单元测试时,我仍在学习绳索。我有一个角度服务,我用它来创建HTML5通知。代码类似于以下内容:

(function() {
  'use strict';

  angular
    .module('blah')
    .factory('OffPageNotification', offPageNotificationFactory);

  function offPageNotificationFactory($window) {
    //Request permission for HTML5 notifications as soon as we can
    if($window.Notification && $window.Notification.permission !== 'denied')     {
      $window.Notification.requestPermission(function (status) { });
    }

    function OffPageNotification () {
      var self = Object.create(OffPageNotification.prototype);
      self.visibleNotification = null;
      return self;
    }


 OffPageNotification.prototype.startNotification = function (options) {
      var self = this;
      self.options = options;
      if(self.options.showHtml5Notification && (!self.options.onlyShowIfPageIsHidden || $window.document.hidden)) {
    if($window.Notification && $window.Notification.permission !== 'denied')     {
          self.visibleNotification = new $window.Notification('Notification',     {
              body: self.options.notificationText,
              icon: self.options.notificationIcon
            });
        }
      }
    };

    .
    .
    .
  return new OffPageNotification();
  }
})();

我正在尝试为此编写单元测试,但我不确定如何模拟$ window.Notification,因此它可以用作构造函数...

self.visibleNotification = new $window.Notification(....)

并且还包含属性

if($window.Notification && $window.Notification.permission !== 'denied')

和方法....

$window.Notification.requestPermission(

我尝试过的一个例子是:

     describe('startNotification', function() {

     beforeEach(function() {
       var mockNotification = function (title, options) {
         this.title = title;
         this.options = options;
         this.requestPermission = sinon.stub();
       };

       mockNotification.prototype.permission = 'granted';

       mockWindow = {
         Notification: new mockNotification('blah', {}),
         document: {hidden: true}
       };

       inject(function (_OffPageNotification_) {
         OffPageNotification = _OffPageNotification_;
       });
     });

     it('should display a html5 notification if the relevant value is true in the options, and permission has been granted', function(){
       var options = {
         showHtml5Notification: true,
         onlyShowIfPageIsHidden: true
       };
       OffPageNotification.startNotification(options);
     });
  });

我收到错误提示' $ window.Notification不是构造函数'有了这个设置,我明白了为什么(我传递了mockNotification的实例化版本)。但是如果我设置了mockWindow.Notification = mockNotification,那么当它调用requestPermission时我会收到一个错误,因为这是未定义的。

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

Notification应该是构造函数。它应该有静态propertiesmethods

mockNotification的所有相关属性都是实例属性,而它们应该是静态的:

function MockNotification() {}

MockNotification.title = title;
MockNotification.options = options;
MockNotification.requestPermission = sinon.stub();

   mockWindow = {
     Notification: MockNotification,
     document: {hidden: true}
   };