本文介绍了使用噶亚军与AngularJS,茉莉,CoffeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了我的应用程序的JRuby / Rails的使用 AngularJS CoffeScript 。我想测试我的JavaScript与茉莉花(又名 Testacular运行),但我得到一个错误,指出我的角模块无处定义。我有什么:安装 Node.js的,生成的配置文件:

My app besides jRuby/Rails uses AngularJS, CoffeScript. I'd like to test my javascript with Jasmine and run it with Karma (aka Testacular), but I get an error stating that my Angular module is nowhere defined. What I've got: installed Node.js and Karma, generated a config file:

// 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 为preprocessor。

I've added AngularJS source files (so module and inject will work), my javascript assets (in CoffeScript) and my specs. I've also added CoffeScript as preprocessor.

我的spec文件:

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。

I'm loading the module with angular.module, injecting dependencies and mocking $http and routeParams.

我的模块定义是这样的:

My module definition looks like this:

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命名空间加载,我suspec这是罪魁祸首。当开始与噶:

The module is loaded in KarmaTracker namespace, I suspec that this is the culprit. When starting Karma with:

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

我可以看到资产已经被编译并服务器D,但我得到一个消息:

I can see that the assets have been compiled and are serverd but I get a message:

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

任何想法,现在该怎么做会更AP preciated!

Any ideas what to do now would be much appreciated!

推荐答案

而不是用野猫加载的资产,你试图通过指定一个接一个,以正确的顺序加载它们?我知道为了果报事做。

Instead of loading the assets with a wildcats, have you tried loading them by specifying one by one, in the correct order? I know the order does matter with karma.

这篇关于使用噶亚军与AngularJS,茉莉,CoffeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:48