Gulp测试找不到模块

时间:2015-02-18 21:11:57

标签: javascript angularjs gulp

我使用gulp angular生成了我的网络应用程序。然后我创建了一个服务并想测试它。

模块文件是app.module.js

angular.module('text', []);

服务文件是text.service.js

(function () {
  'use strict';
  angular.module('text')
    .factory('description',TextService);

  TextService.$inject = ['$http']

  function TextService($http) {

    return {
      QueryStaticText: queryStaticText

  };

    function queryStaticText(link) {
      return link;
    }
  }

})();

测试文件是text.service.spec.js

'use strict';

describe('TextService', function () {
  var description;

  beforeEach(function () {

    module('text');

    inject(function (_description_) {
      description = _description_;
    });

  });

  it('should return text', function () {
    expect(description.QueryStaticText("Hello")).toEqual("Hello anu");
  });
});

在控制台中我执行gulp测试,我收到错误消息

[22:02:44] Using gulpfile /Volumes/Developer/angularjs/project/gulpfile.js
[22:02:44] Starting 'test'...
[22:02:45] Starting Karma server...
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket 1KW_uYCk45moVKwfgAd2 with id 19804685
PhantomJS 1.9.8 (Mac OS X) ERROR
  Error: [$injector:nomod] Module 'text' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
  http://errors.angularjs.org/1.3.12/$injector/nomod?p0=text
  at /Volumes/Developer/angularjs/project/bower_components/angular/angular.js:1769



/Volumes/Developer/angularjs/project/gulp/unit-tests.js:30
      throw err;

未加载文本模块,如何加载它?

3 个答案:

答案 0 :(得分:2)

看起来你没有在Karma中按顺序加载你的文件。在你里面karma.conf.js应该有一个文件列表。加载应用程序模块,然后加载其余的JavaScript。这是一篇包含same problem的帖子。

例如:

files: [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-animate/angular-animate.js',

  'app/**/*.module.js',  // Loads all files with .module.js
  'app/**/*.js',         // Load the rest of your .js angular files
  'test/**/*.js'         // Load your tests
],

答案 1 :(得分:1)

使用angularFilesort按依赖顺序排序文件:

// gulp/unit-tests.js
...
// around line 36 inserted the following line:
.pipe($.angularFilesort())

所以应该看起来像:

gulp.src(srcFiles)
  .pipe($.angularFilesort())
  .pipe(concat(function(files) {
    callback(bowerDeps.js
      .concat(_.pluck(files, 'path'))
      .concat(htmlFiles)
      .concat(specFiles));
  }))

答案 2 :(得分:0)

beforeEach(module('text'));
beforeEach(inject(function (_description_) {
  description = _description_;
}));

基本上,模块和注入的返回值必须是beforeEach的参数。见https://docs.angularjs.org/guide/module#unit-testing