3
我的應用程序除了jRuby/Rails
使用AngularJS
,CoffeScript
。我想用Jasmine
測試我的javascript並使用Karma
(又名Testacular
)運行它,但是我收到一個錯誤,指出我的Angular模塊沒有定義。我有什麼:安裝Node.js
和Karma
,生成的配置文件:與AngularJS,Jasmine,CoffeScript一起使用Karma-runner
// 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
作爲預處理器。
我的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。
我的模塊定義是這樣的:
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這是罪魁禍首。當開始噶:
karma start --log-level debug config/karma.conf.js
我可以看到,資產已編譯和服務器D是,但我得到一個消息:
PhantomJS 1.8 (Linux) ERROR
ReferenceError: Can't find variable: KarmaTracker
at /home/.../KarmaTracker/app/assets/javascripts/account.js.coffee-compiled.js:1
任何想法,現在該怎麼做,將不勝感激!