为什么我收到消息说 provider must return value from factory $get method?

Why am I getting message that provider must return value from factory $get method?

所以,我有一套针对 angular 的测试,使用 jasmine 和 karma。我有一个像这样定义的 lodash 和 moment 模块

(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);

这里是一个业力文件(不是完整的文件,只是处理文件加载的部分)

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

我在评论中冒险了,经过一番挖掘,我相信我是对的。 mock injector.

正在测试中从您的服务名称中删除“_”

具体来说,请阅读 Resolving References (Underscore Wrapping):

部分

To help with this, the injected parameters can, optionally, be enclosed with underscores. These are ignored by the injector when the reference name is resolved.

我似乎找不到执行此操作的实际 source code。然而,我的诊断似乎得到了以下两个事实的支持:

  • 您使用相同的技术为 moment.js 声明服务,没有任何问题
  • 问题只发生在测试中,其中模拟注入器 uses/supports 下划线换行。

编辑:

测试的潜在解决方法:

在创建控制器时的测试代码中,使用下划线包裹技术来检索您的 lodash 服务,如下所示:

var controller, scope;
beforeEach(inject(function($rootScope, $controller, ___) {
  scope = $rootScope.$new();
  controller = $controller('batchSearchCtrl', {$scope: scope, _: ___});
}));