Karma / Jasmine找不到所需文件

时间:2017-01-03 13:44:41

标签: angularjs jasmine karma-runner karma-jasmine

在这个Angular / Jasmine测试中,我有一个使用名为http的服务的控制器。该服务位于http.js文件夹中名为services的文件中。

问题(见下面的错误)是Karma / Jasmine没有找到http服务,即使我在karma.conf.js中明确声明了它。可能是什么问题?

PhantomJS 2.1.1 (Windows 8 0.0.0) Testing Bank management should retrieve bank list FAILED

Error: [$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=httpProvider%20%3C-%20http (line 41)

        libs/angular/angular.min.js:41:146
        d@libs/angular/angular.min.js:39:93
        libs/angular/angular.min.js:41:198
        d@libs/angular/angular.min.js:39:93
        e@libs/angular/angular.min.js:39:363
        workFn@libs/angular/angular-mocks.js:2439:26
        inject@libs/angular/angular-mocks.js:2411:41
        test/web/admin/banks/mngbanks.test.js:44:10
        loaded@http://localhost:9876/context.js:151:17
        undefined
        TypeError: undefined is not an object (evaluating 'deferred.resolve') in test/web/admin/banks/mngbanks.test.js (line 70)
        test/web/admin/banks/mngbanks.test.js:70:12
        loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 3 of 3 (1 FAILED) (0.016 secs / 0.021 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: 'c:/apps/public/',
    frameworks: ['jasmine'],
    files: [
        'libs/angular/angular.min.js',
        'libs/angular/angular-mocks.js',
        'app/web/app.js',
        'app/services/http.js',    // <-- service is in this file
        'app/**/*.js',
        'test/**/*.js'
    ],
    exclude: [

    ],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: true,
    concurrency: Infinity
  })
}

茉莉花测试:

describe('Testing management', function () {
    var $scope;
    var deferred;

    var list = {
            data: {
                acc: 2,
                bnks: [
                       {sk:1, nm:"Bank 1", dsc:"Bank 1 description",als:"BK1"},
                       {sk:2, nm:"Bank 2", dsc:"Bank 2 description",als:"BK2"},
                       {sk:3, nm:"Bank 3", dsc:"Bank 3 description",als:"BK3"}
                ]
            }
        };

    var ConstantsMock = {
        ReadMany: 'M'
    };

    function NgTableParamsMock() {
    }

    beforeEach(function () {
       module(moduleName);
       module(function ($provide) {
             $provide.value('NgTableParams', NgTableParamsMock);
             $provide.value('Constants', ConstantsMock);
          });

      inject(function($controller, _$rootScope_, $q, http, $timeout) {

        $scope = _$rootScope_.$new();
        console.log("scope:"+$scope)
        deferred = $q.defer();
        spyOn(http, 'call').and.returnValue(deferred.promise);
        $controller('mngBanksCtl', { 
            $rootScope: _$rootScope_,
            $scope: $scope,
            $timeout: $timeout,
            http: http,
            NgTableParams: NgTableParamsMock,
            Constants: ConstantsMock
        });
      });

  });

  it('should retrieve list', function () {

    deferred.resolve(list);
    $scope.$apply();
    expect($scope.data).toBe(list);
  });
});

1 个答案:

答案 0 :(得分:0)

问题是注入服务的依赖关系未包含在.js文件列表中。我发现这是通过创建一个Jasmine隔离测试(没有Karma)告诉我错误,Karma没有