Angular Directive Testing templateUrl

时间:2016-01-05 02:42:06

标签: javascript angularjs angularjs-directive jasmine webstorm

我正在学习如何以角度进行测试。我一直试图让测试运行在使用templateUrl的指令上。我正在使用Webstorm和Karma和Jasmine。我有两个错误,我试图修复:

'WARNING: Tried to load angular more than once.'
'WARNING: Tried to load angular more than once.'
'WARNING: Tried to load angular more than once.'

Error: [$injector:modulerr] Failed to instantiate module projectStatus due to:
Error: State 'dashboard'' is already defined

我已经尝试了一些食谱,但我所做的一切都没有改变这两个错误。这就是我现在所设置的内容:

routes.js

    angular.module('projectStatus').config(['$urlRouterProvider', '$stateProvider',
  function($urlRouterProvider, $stateProvider, Auth){
  $urlRouterProvider.otherwise("/dashboard/projects/list/current/All");
$stateProvider
  .state("dashboard",{
    url:"/dashboard",
    templateUrl: "views/layout/dashboard.html",
    controller: function($scope, $state, $stateParams) {
      $scope.$state = $state;
      var cleanup = $scope.$on("state:change", function(e, type) {
        $scope.stateType = type;
      });
      $scope.$on("destroy", function(){
        cleanup();
      });
    },
    abstract: true
  })
  .state("dashboard.addproject", {
    url:"/addproject/:department",
    templateUrl: "views/pages/addproject.html",
    data: {
      role: "admin"
    }
  })
}]);

karma.conf.js

module.exports = function(config) {
  config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
  'node_modules/jquery/dist/jquery.js',
  'node_modules/angular/angular.js',
  'node_modules/angular-mocks/angular-mocks.js',
  'js/**/*.js',
  'views/**/*.html',
  'test/**/*.js'
],

// list of files to exclude
exclude: [
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
  'views/**/*.html': ['ng-html2js']
},

ngHtml2JsPreprocessor: {
  moduleName: 'templates'
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'IE', 'Firefox'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultanous
concurrency: Infinity
  })
};

pssAddProject.js

angular.module('projectStatus').directive('pssAddProject',  function(MilestoneData, ProjectsData, Project, ProjectDataApi,  projectTypeData, $state, $timeout, DepartmentData, $stateParams){
  return {
    restrict: "C",
    replace: true,
    templateUrl: "views/directives/pssAddProject.html",
    link: function(scope, element, attrs){

pssAddProject.spec.js

describe("pssAddProject", function(){
  var element, $scope;

  beforeEach(module('projectStatus'));
  beforeEach(module('templates'));

  beforeEach(inject(function ($compile, $rootScope) {
      //create a scope
      $scope = $rootScope.$new();

      //get the jqLite or jQuery element
      element = angular.element('<div class="pss-add-project"></div>');

      //compile the element into a function to process view
      $compile(element)($scope);

      //call digest on the scope
      $rootScope.$digest();

  }));

  it('should render the directive out into the dom', function(){
    expect(element.find(".pss-add-project")).toEqual(1);
  });

});

应用程序运行没有错误。在尝试测试指令时,我只得到上述两个错误。我已经查看了几个关于&#39;警告的帖子:尝试不止一次加载角度。&#39;但我没有找到与测试有关的错误。

0 个答案:

没有答案