角度单元测试工厂注射

时间:2016-10-25 19:26:59

标签: angularjs unit-testing karma-jasmine

我已经看到了这个问题,但没有一个解决方案适合我。我收到错误if (noun.contains("man")) { pluralWord = noun.replace("man", "men"); break; } 导致我的测试失败。将工厂注入不是存根工厂的测试规范的正确方法是什么。

登录模块

Error: [ng:areq] Argument 'fn' is not a function, got undefined

登录控制器

angular.module('loginModule', ['ui.bootstrap', 'permission', 'ui.router', 'coreModule', 'utilsModule']);

登录控制器规范

  (function(){
      'use strict';

      angular.module('loginModule')
      .controller('loginController', login);

      login.$inject = [
          '$log',
          '$uibModal',
          '$rootScope',
          'storageFactory',
          'loginFactory',
          '$state',
          'RoleStore',
          'PermissionStore',
          'vsmsCoreFactory'
      ];

      function login($log, $uibModal, $rootScope, storageFactory, loginFactory, $state, RoleStore, PermissionStore, vsmsCoreFactory) {

          /* jshint validthis: true */
          var vm = this;
          vm.loginUser = loginUser;
          vm.forgotPassword = forgotPassword;
          vm.errorCode = null;
          PermissionStore.clearStore();
          vsmsCoreFactory.getHashFunction()
            .then(function(response) {
                if(response) {
                  storageFactory.setHashFunction(response); // unknown provider
              }
            });

          function loginUser() {
          ...

单元测试文件

describe('loginController', function() {

  var $controller;

  beforeEach(module('loginModule'));

  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  describe('vm.loginUser', function() {
    it('should be defined', function() {
      var loginController = $controller('loginController');
      expect(loginController.loginUser).toBeDefined();
    });
  });

});

应用

'use strict';

var gulp = require('gulp');

var $ = require('gulp-load-plugins')();

var wiredep = require('wiredep');

var paths = gulp.paths;

function runTests (singleRun, done) {
  var bowerDeps = wiredep({
    directory: 'bower_components',
    exclude: ['bootstrap-sass-official'],
    dependencies: true,
    devDependencies: true
  });

  var testFiles = bowerDeps.js.concat([
    './src/components/scripts/ui-bootstrap-custom-tpls-2.1.3.js',
    './src/app/index.js',
    './src/{app,components}/**/*.module.js',
    './src/{app,components}/**/*.factory.js',
    './src/{app,components}/**/*.controller.js',
    './src/{app,components}/**/*.spec.js'
  ]);

  gulp.src(testFiles)
    .pipe($.karma({
      configFile: 'karma.conf.js',
      action: (singleRun)? 'run': 'watch'
    }))
    .on('error', function (err) {
      // Make sure failed tests cause gulp to exit non-zero
      throw err;
    });
}

gulp.task('test', function (done) { runTests(true /* singleRun */, done) });
gulp.task('test:auto', function (done) { runTests(false /* singleRun */, done) });

登录

Chrome 54.0.2840(Mac OS X 10.11.6)loginController vm.loginUser应定义为FAILED     错误:[$ injector:unpr]未知提供者:storageFactoryProvider< - storageFactory< - loginController

1 个答案:

答案 0 :(得分:1)

未为测试加载fotaAdminPortal模块,因此vsmsCoreFactory永远不会注册。

此外,无需手动将服务传递到控制器,因为注入器会在您创建时自动执行此操作。当您需要传递非全局注册服务(即$scope或模拟单个控制器实例的服务)时,手动传递服务非常有用。

Atm工厂的模拟只是一个未定义的变量,当依赖于它们的逻辑尝试调用不存在的方法时会导致错误。你需要在这些模拟中正确地存根任何方法。但暂时不考虑那些你的测试应该是这样的:

describe('loginController', function() {

  var $controller;

  beforeEach(module('fotaAdminPortal'));

  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  describe('vm.loginUser', function() {
      it('should be defined', function() {
        var loginController = $controller('loginController');
        expect(loginController.loginUser).toBeDefined();
      });
  });

});

如果要单独加载loginModule,则需要将vsmsCoreFactory提取到另一个模块中,并将其作为依赖项添加:

angular.module('vsmsCore', [])
  .factory('vsmsCoreFactory', function($http, env, $log, storageFactory) {

  });

angular.module('loginModule', ['ui.bootstrap', 'permission', 'ui.router', 'vsmsCore']);

然后你可以改为beforeEach(module('loginModule'))