Using Karma-runner with AngularJS, Jasmine, CoffeScript

巧了我就是萌 提交于 2019-12-31 01:56:15

问题


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'
};

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.

My spec file:

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');
  });
});

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) ->
.....

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

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

Any ideas what to do now would be much appreciated!


回答1:


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.



来源:https://stackoverflow.com/questions/17601744/using-karma-runner-with-angularjs-jasmine-coffescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!