【问题标题】:angular is not defined error when karma test invoked调用业力测试时未定义角度错误
【发布时间】:2017-06-20 02:28:10
【问题描述】:

我确定这与 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'));
  }));


});

项目文件结构:

【问题讨论】:

    标签: javascript angularjs karma-jasmine


    【解决方案1】:

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

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

    【讨论】:

    • 是的,我已经安装了所有依赖项并且我也看到了文件angular-mocks.js
    • 它可能是你的 basePath。除非你改变了它周围的一些文件夹,否则应该留空。
    • 我没有对项目结构进行任何更改,因为这是我的第一个 Angular 项目,我不想进行我不熟悉的愚蠢更改
    • 确实如此。这就是问题所在。抱歉这么晚才回来。谢谢jdewit!
    猜你喜欢
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 2018-07-01
    相关资源
    最近更新 更多