3

我的应用程序除了jRuby/Rails使用AngularJS, CoffeScript。我想用(aka ) 测试我的 javascriptJasmine并运行它,但我收到一条错误消息,指出我的Angular 模块未定义。我所拥有的:安装并生成了一个配置文件:KarmaTestacularNode.jsKarma

// base path, that will be used to resolve files and exclude
basePath = '../';


// list of files / patterns to load in the browser
files = [
  JASMINE,
  JASMINE_ADAPTER,
  'vendor/assets/javascripts/angular.js',
  'vendor/assets/javascripts/angular-mocks.js',
  'vendor/assets/javascripts/angular-resource.js',
  'app/assets/javascripts/*.js.coffee',
  'spec/javascripts/*_spec.js'
];

preprocessors = {
  '**/*.coffee': 'coffee'
};

我已经添加了AngularJS源文件(因此模块和注入将起作用)、我的 javascript 资产(在 中CoffeScript)和我的规范。我还添加CoffeScript了预处理器。

我的规格文件:

describe("PasswordResetsController", function() {
  //Mocks
  var http = {};
  var routeParams = {};

  //Controller
  var ctrl = null;

  //Scope
  var $scope = null;

  beforeEach( function() {
    angular.module('KarmaTracker');
  });

  /* IMPORTANT!
   * this is where we're setting up the $scope and
   * calling the controller function on it, injecting
   * all the important bits, like our mockService */
  beforeEach(angular.inject(function($rootScope, $httpBackend, $controller) {
    //create a scope object for us to use.
    $scope = $rootScope.$new();

    //http mock from Angular
    http = $httpBackend;


    //now run that scope through the controller function,
    //injecting any services or other injectables we need.
    ctrl = $controller(PasswordResetsController, {
      $scope: $scope,
      $http: http,
      $routeParams: routeParams
    });
  }));


  it("foo spec", function() {
    expect($scope.foo).toEqual('test');
  });
});

我正在使用 angular.module 加载模块,注入依赖项并模拟 $http 和 routeParams。

我的模块定义如下所示:

window.KarmaTracker = angular.module('KarmaTracker', ['ngCookies', 'ngMobile'])

# Flashe message passed from other controllers to FlashesController
KarmaTracker.factory "FlashMessage", ->
  { string: "", type: null }

KarmaTracker.controller "RootController", ($scope, $http, $location, $cookies, $routeParams, FlashMessage, broadcastService) ->
.....

该模块加载在 KarmaTracker 命名空间中,我怀疑这是罪魁祸首。启动 Karma 时:

karma start --log-level debug config/karma.conf.js

我可以看到资产已编译并已服务器,但我收到一条消息:

PhantomJS 1.8 (Linux) ERROR
        ReferenceError: Can't find variable: KarmaTracker
        at /home/.../KarmaTracker/app/assets/javascripts/account.js.coffee-compiled.js:1

任何想法现在做什么将不胜感激!

4

1 回答 1

0

您是否尝试过以正确的顺序一一指定来加载资产,而不是使用通配符加载资产?我知道顺序对业力很重要。

于 2014-01-25T14:34:31.293 回答