2016-12-27 92 views
0

版本噶角1.5錯誤:alertify.dialog:運行至少兩個測試時

角名已存在:1.5.0

alertify-JS:1.6.1

我會喜歡在Angular 1.5組件上實現單元測試。

我創建了karma.conf.js,進口依賴我:

//jshint strict: false 
module.exports = function(config) { 
    config.set({ 

     basePath: './', 

     files: [ 
      'app/bower_components/jquery/dist/jquery.js', 
      'app/bower_components/angular/angular.js', 
      'app/bower_components/angular-ui-router/release/angular-ui-router.js', 
      'app/bower_components/angular-mocks/angular-mocks.js', 
      'app/bower_components/kendo-ui/src/js/kendo.ui.core.js', 
      'app/bower_components/kendo-ui/src/js/kendo.angular.js', 
      'app/bower_components/kendo-ui/src/js/cultures/kendo.culture.fr-FR.js', 
      'app/bower_components/alertify-js/build/alertify.js', 
      'app/bower_components/angular-resource/angular-resource.js', 
      'app/bower_components/ui-leaflet/dist/ui-leaflet.js', 
      'app/bower_components/angular-simple-logger/dist/angular-simple-logger.js', 
      'app/bower_components/lodash/lodash.js', 
      'app/bower_components/moment/moment.js', 
      'app/bower_components/moment/locale/fr.js', 
      'app/modules/app.js', 
      'app/modules/**/*.md.js', 
      'app/modules/**/*.js', 
      'test/**/*.js' 
     ], 

     autoWatch: false, 

     frameworks: ['jasmine'], 

     browsers: ['Chrome'], 

     plugins: [ 
      'karma-chrome-launcher', 
      'karma-firefox-launcher', 
      'karma-jasmine', 
      'karma-junit-reporter' 
     ], 

     singleRun: true, 

     reporters: ['dots', 'junit'], 

     junitReporter: { 
      outputFile: 'test-results.xml' 
     } 

    }); 
}; 

下面是測試情況下,將重現錯誤文件的最小工作爲例。它只需要進行兩項測試:

'use strict'; 

describe('Component: sales', function() { 
    var $componentController; 
    var $scope; 

    beforeEach(module('app.sales')); 
    beforeEach(inject(function (_$componentController_, $rootScope, _TownshipService_, $q) { 
     $scope = $rootScope.$new(); 
     $componentController = _$componentController_('sales', {$scope: $scope, TownshipService: _TownshipService_}); 
    })); 

    describe('controller', function() { 

     it('should be defined', function() { 
      expect($componentController).toBeDefined(); 
     }); 

     it('should not crash', function() { 

     }); 
    }); 
}); 

我因alertify而崩潰。 We created a new dialog following the default usage of the documentation,我們稱之爲auxConfirm

(function (app) { 
    'use strict'; 

    app.run(function() { 
     alertify.dialog('auxConfirm', function() { 
      // [...] 
     }); 
    }); 
})(angular.module('app.component')); 

我們得到以下錯誤信息:

Chrome 55.0.2883 (Mac OS X 10.12.2) Component: sales controller FAILED 
    Error: alertify.dialog: name already exists 
    at Object.dialog ([project_folder]/ui/app/bower_components/alertify-js/build/alertify.js:2885:27) 
     at [project_folder]/ui/app/modules/common/component/ThreeButtonsConfirm.js:5:18 
     at Object.invoke ([project_folder]/ui/app/bower_components/angular/angular.js:4604:19) 
     at [project_folder]/ui/app/bower_components/angular/angular.js:4412:62 
     at forEach ([project_folder]/ui/app/bower_components/angular/angular.js:321:20) 
     at Object.createInjector [as injector] ([project_folder]/ui/app/bower_components/angular/angular.js:4412:3) 
     at Object.workFn ([project_folder]/ui/app/bower_components/angular-mocks/angular-mocks.js:2799:52) 
Chrome 55.0.2883 (Mac OS X 10.12.2): Executed 2 of 2 (1 FAILED) (0.073 secs/0.058 secs) 

通過觀察alertify源代碼,當它試圖注入新的自定義對話框中,我們檢查自定義對話框的名稱,並拋出一個異常如果它已經存在:

  /** 
      * Dialogs factory 
      * 
      * @param {string}  Dialog name. 
      * @param {Function} A Dialog factory function. 
      * @param {Boolean}  Indicates whether to create a singleton or transient dialog. 
      * @param {String}  The name of the base type to inherit from. 
      */ 
      dialog: function (name, Factory, transient, base) { 

       // get request, create a new instance and return it. 
       if (typeof Factory !== 'function') { 
        return get_dialog(name); 
       } 

       if (this.hasOwnProperty(name)) { 
        throw new Error('alertify.dialog: name already exists'); 
       } 
      } 

我該怎麼做才能避免出現此錯誤?我沒有找到一種方法從alertify中刪除自定義對話框,我可以放入afterEach()。有沒有辦法重新創建alertify.js來防止這種情況?

回答

0

該解決方案出現在auxConfirm聲明文件中。在alertify.dialog()解決問題之前添加條件語句。

(function (app) { 
    'use strict'; 

    app.run(function() { 
     if (!alertify.auxConfirm) { 
      alertify.dialog('auxConfirm', function() { 
       // [...] 
      }); 
     } 
    }); 
})(angular.module('app.component')); 
相關問題