除了jRuby/Rails,我的应用程序还使用了AngularJS,CoffeScript。我想用Jasmine测试我的javascript,并使用Karma (又名Testacular)运行它,但是我发现一个错误,说明我的角模块没有定义。我得到的是:安装了Node.js和Karma,生成了一个配置文件:
// 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 start --log-level debug config/karma.conf.js我可以看到这些资产已经被编译,并且是serverd的,但是我得到了一个信息:
PhantomJS 1.8 (Linux) ERROR
ReferenceError: Can't find variable: KarmaTracker
at /home/.../KarmaTracker/app/assets/javascripts/account.js.coffee-compiled.js:1任何想法,现在做什么,将是非常感谢!
发布于 2014-01-25 14:34:31
您是否尝试过按正确的顺序逐个指定资产,而不是用野猫来加载它们呢?我知道秩序与因果报应有关。
https://stackoverflow.com/questions/17601744
复制相似问题