所以,我有一套角度测试,使用茉莉和业力。我有一个用于lodash的模块和像这样定义的时刻
(function (angular) {
var lodash = angular.module('lodash', []);
lodash.factory('_', ['$window', function ($window) {
//lodash must already be loaded on the page
var _ = $window._;
delete ($window._);
return _ ;
}]);
var moment = angular.module('moment', []);
moment.factory('moment',['$window',function ($window) {
var toReturn = $window.moment;
delete ($window.moment);
return toReturn;
}]);
})(angular);
这一切在我的应用程序中都没有问题。为什么我会收到这条消息,当我虔诚地回来时,我会回复一个" _"方法中的对象?
使用示例
(function (angular) {
'use strict';
var controllerId = 'batchSearchCtrl';
angular.module('app').controller(controllerId, batchSearchCtrl);
batchSearchCtrl.$inject = ['$scope', '$routeParams', 'batchSearchViewModel', 'modalSvc', '_'];
function batchSearchCtrl($scope, $routeParams, batchSearchViewModel, modalSvc, _ ) {
}
})(angular);
这是一个karma文件(不是完整文件只是处理文件加载的部分)
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '../app',
// frameworks to use
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.js'
, 'http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular-resource.js'
, 'http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular-route.js'
, 'http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular-animate.js'
, '../node_modules/angular-mocks/angular-mocks.js'
, '../components/**/*.js'
, '../scripts/sinon-1.7.3.js'
, '../scripts/base64.js'
, '../scripts/moment-2.4.0.js/moment-with-langs.js'
, '../scripts/watch-1.3.0.js/watch.js'
, '../scripts/jquery/jquery.js'
, '../scripts/bootstrap/bootstrap.js'
, '../scripts/ui-bootstrap/ui-bootstrap-tpls-0.10.0.js'
, '../scripts/lodash/lodash-3.0.0.js'
, '../scripts/watch/watch-1.3.0.js'
, '../scripts/scriptModules.js'
, '../common/module.js'
, 'app.js'
, '../common/*.js'
, '../common/**/*.js'
, '*.js' // all js files in root app folder
, 'app_directives/**/*.js'
, 'batch/*.js'
, 'batch/closing/*.js'
, 'batch/search/*.js'
, 'batch/summary/*.js'
});
};
答案 0 :(得分:0)
我在评论中走了出来,经过一番挖掘,我相信我是对的。 " _"正在通过mock injector在测试中删除您的服务名称。
具体来说,请阅读Resolving References (Underscore Wrapping)部分:
为了帮助解决这个问题,注入的参数可以选择用下划线括起来。解析引用名称时,注入器会忽略这些内容。
我似乎无法找到正在执行此操作的实际source code。但是,我的诊断似乎得到了这两个事实的支持:
修改强>
潜在的测试工作:
在创建控制器的测试代码中,使用下划线包装技术来检索您的lodash服务,如下所示:
var controller, scope;
beforeEach(inject(function($rootScope, $controller, ___) {
scope = $rootScope.$new();
controller = $controller('batchSearchCtrl', {$scope: scope, _: ___});
}));