1

I generated my web application with gulp angular. Then I created a service and want to test it.

Module file is app.module.js

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

Service file is 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;
    }
  }

})();

Test file is 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");
  });
});

In the console I execute gulp test and I've got the error message

[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;

The text module is not loaded, how can I load it?

Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
softshipper
  • 32,463
  • 51
  • 192
  • 400

3 Answers3

2

It looks like you are not loading your files in order in Karma. Inside of you karma.conf.js there should be a file list. Load the app module then the rest of your javascript. Here is a post that had the same problem.

For Example:

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
],
Community
  • 1
  • 1
angmerica
  • 787
  • 8
  • 13
1

Use angularFilesort to order files by dependency:

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

So should look like:

gulp.src(srcFiles)
  .pipe($.angularFilesort())
  .pipe(concat(function(files) {
    callback(bowerDeps.js
      .concat(_.pluck(files, 'path'))
      .concat(htmlFiles)
      .concat(specFiles));
  }))
Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
onblur
  • 365
  • 1
  • 5
  • 16
0
beforeEach(module('text'));
beforeEach(inject(function (_description_) {
  description = _description_;
}));

Basically, the return value of your module and injection need to be the argument for beforeEach. See https://docs.angularjs.org/guide/module#unit-testing

JSTNJNS
  • 41
  • 3
  • I try it, but does not help anything. Details description https://github.com/Swiip/generator-gulp-angular/issues/364#issuecomment-75015636 – softshipper Feb 19 '15 at 10:14