使用jasmine和多个依赖项测试angularjs服务时出现问题

时间:2015-08-10 16:54:45

标签: angularjs unit-testing jasmine

我有一个服务有很多依赖项定义为:

angular.module('serversync.services', ['database', 'date-time-format', 'serversync.config'])

.factory('ServerSync', function($log, $q, $http, _, DatabaseProvider, DateTimeFormatter, ExternalSyncServerConfig) {
    var self = this;

    var self.syncSomething = function() {
       ...
    };
    return self;

});

(DatabaseProvider,DateTimeFormatter,ExternalSyncServerConfig是我应用中的其他自定义服务)。

我已经定义了一个基本测试如下:

describe('ServerSync Tests', function () {

  beforeEach(function() {
    module('database', []);
    module('date-time-format', []);
    module('serversync.config', []);
    module('serversync.services')
  });

  var ServerSyncSvc;
  var log, q, http, underscore;
  var DatabaseProviderSvc, DateTimeFormatterSvc, ExternalSyncServerConfigSvc;

  beforeEach(function() {
    inject(function($log, 
                    $q, 
                    $httpBackend, 
                    DatabaseProvider, 
                    DateTimeFormatter, 
                    ExternalSyncServerConfig, 
                    ServerSync) {
      ServerSyncSvc = ServerSync;
      log = $log;
      q = $q;
      http = $httpBackend;
      underscore = _;
      DatabaseProviderSvc = DatabaseProvider;
      DateTimeFormatterSvc = DateTimeFormatter;
      ExternalSyncServerConfigSvc = ExternalSyncServerConfig;
    });
  });

  it('should have a syncJourney function defined', function () {
    expect(ServerSyncSvc.syncSomething).toBeDefined();
  });

})
;

我已经使用this article尝试启动并运行但没有成功。 当我运行grunt karma时,我收到错误:

PhantomJS 1.9.8 (Mac OS X) Service DTW should have a function to be defined FAILED
    Error: [$injector:modulerr] Failed to instantiate module undefined due to:
    Error: [ng:areq] Argument 'fn' is not a function, got undefined
    http://errors.angularjs.org/1.3.13/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20undefined
        at assertArg (~/myApp/app/bower_components/angular/angular.js:1580)
        at assertArgFn (~/myApp/app/bower_components/angular/angular.js:1591)
        at annotate (~/myApp/app/bower_components/angular/angular.js:3471)
        at ~/myApp/app/bower_components/angular-mocks/angular-mocks.js:2147
        at invoke (~/myApp/app/bower_components/angular/angular.js:4163)
        at ~/myApp/app/bower_components/angular/angular.js:4104
        at forEach (~/myApp/app/bower_components/angular/angular.js:323)
        at loadModules (~/myApp/app/bower_components/angular/angular.js:4123)
        at createInjector (~/myApp/app/bower_components/angular/angular.js:4007)

我知道我的Jasmine conf是正确的,因为我可以对没有任何依赖项的简单服务运行一些测试。 对于角度来说整个单元测试的东西对我来说有点新鲜,我在围绕模拟依赖关系的概念时遇到了一些麻烦所以我希望它很简单,但我不知道下一步要去哪里这工作吗?

1 个答案:

答案 0 :(得分:0)

在设置beforeEach中 - 将依赖关系的模块更改为angular.module,而不仅仅是module

angular.module('database', []);
angular.module('date-time-format', []);
angular.module('serversync.config', []);
module('serversync.services')

模块用于您正在测试的任何组件(它是模拟版本angular.mock.module的快捷方式)。依赖项应该注册为常规角度模块。

这可能足以让你的“被定义”单元测试运行。但是,一旦您实际执行使用这些依赖项的代码,您将不得不为依赖项中的函数提供模拟。如果你像我一样,那将带来一系列更多的问题。