繁体   English   中英

调用karma测试时,angular未定义错误

[英]angular is not defined error when karma test invoked

我确定这与angular-mock.js有关但我无法想象我需要做什么,因为一切似乎都很好。 我只是使用angular-seed应用程序的默认设置。 请帮忙摆脱这个问题

karma.conf.js

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

    basePath : '../',

    files : [
      'bower_components/angular/angular.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'app/js/**/*.js',
      'test/unit/**/*.js'
    ],

    autoWatch : true,

    frameworks: ['jasmine'],

    browsers : ['Chrome'],

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

    junitReporter : {
      outputFile: 'test_out/unit.xml',
      suite: 'unit'
    }

  });
};

controllers.js

'use strict';
/* Controllers */

var app = angular.module('myApp.controllers', []);
app.constant('RESTURL', 'http://'+ location.hostname + ':8003');

app.controller('MainCtrl', ['$scope', 'dataService', 'flash', 'mySharedService','facetSearchService', 'facetSelectionService', 'RESTURL',  function($scope, dataService, flash, sharedService, facetSearch, facet, RESTURL) {
      $scope.RESTURL =  RESTURL;
      $scope.loading = true;
      $scope.data = null;
      $scope.resultCount = 0;
      $scope.currentPage = 0;
      $scope.pageSize = 10;

          ....
])}

controllerSpec.js

'use strict';

/* jasmine specs for controllers go here */

describe('controllers', function(){
  beforeEach(module('myApp.controllers'));


  it('should test MainCtrl', inject(function($controller) {
    //spec body
    var scope = {},
    ctrl = $controller('MainCtrl', {$scope:scope});

    expect($scope.RESTURL).toBe('http://'+ location.hostname + ':8003'));
  }));


});

项目文件结构: 在此输入图像描述

我相信你运行“bower install”来安装依赖项?

bower_components的路径不正确。 “.../”的基本路径将使karma查看项目的根目录,但您的bower_components位于“app”文件夹中。 karma.conf中的“文件”需要以“app”为前缀。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM