Testing service in Angular returns module is not defined

后端 未结 7 1200
借酒劲吻你
借酒劲吻你 2020-12-13 03:11

I am trying to run the default service unit test in my project (Taken from the Angular Seed project on GitHub), but I keep getting the error \"module is not defined\".

相关标签:
7条回答
  • 2020-12-13 03:33

    We use 'module' without 'angular' in our unit tests and it works fine.

    CoffeeScript:

    describe 'DiscussionServicesSpec', ->
        beforeEach module 'DiscussionServices'
        beforeEach inject ... etc.
    

    which compiles to

    JavaScript:

    describe('DiscussionServices', function() {
        beforeEach(module('DiscussionServices'));
        beforeEach(inject(function ... etc.
    

    The only time I see something like the error you described is if in the testacular.conf.js file the angular-mocks.js file is not listed in the files section before the specs trying to use 'module'. If I put it after my tests in the 'files' list I get

    ReferenceError: Can't find variable: module
    

    (Our tests are being run through PhantomJS)

    0 讨论(0)
  • 2020-12-13 03:34

    I had the same problem, and I understood why it wasn't working: The jasmine.js javascript must be referenced BEFORE the angular-mocks.js file. Indeed, the angular-mocks.js checks if Jasmine is loaded, and only if it is it will add the module function to the window.

    Here is an extract of Angular Mocks code:

    (Edit after the few comments about 'hacking' I had below: this is just an extract of the code, this is not something you need to write yourself, it's already there!)

    window.jasmine && (function(window) {
    
    [...]
    
      window.module = angular.mock.module = function() {
        var moduleFns = Array.prototype.slice.call(arguments, 0);
        return isSpecRunning() ? workFn() : workFn;
        /////////////////////
        [...]
      };
    

    In a nutshell: Just reference your jasmine.js before angular-mocks.js and off you go.

    0 讨论(0)
  • 2020-12-13 03:40

    You are missing the angular-mocks.js file.

    0 讨论(0)
  • 2020-12-13 03:44

    I had this same issue when I was doing something like var module = angular.module('my',[]). I needed to make sure it was surrounded by IIFE

    0 讨论(0)
  • 2020-12-13 03:45

    The window.module function comes in angular-mocks.js and is a shorthand for angular.mock.module. As mentioned in the docs, the module function only works with Jasmine.

    Using Testacular, the following example configuration file will load angular-mocks.js.

    /** example testacular.conf.js */
    
    basePath = '../';
    files = [
      JASMINE,
      JASMINE_ADAPTER,
      'path/to/angular.js',
      'path/to/angular-mocks.js',   // for angular.mock.module and inject.
      'src/js/**/*.js',             // application sources
      'test/unit/**/*.spec.js'      // specs
    ];
    autoWatch = true;
    browsers = ['Chrome'];
    

    And, as suggested elsewhere, you can run Testacular with debug logging to see what scripts are loaded (you can also see the same in the inspector):

    testacular --log-level debug start config/testacular.conf.js
    

    The angular.mock.inject docs include a pretty complete example.

    0 讨论(0)
  • 2020-12-13 03:47

    I had included angular-mocks.js in my karma config, but was still getting the error. It turns out the order is important in the files array. (duh) Just like in the head of an html doc, if a script calls angular before it's defined, and error occurs. So I just had to include my app.js after angular.js and angular-mocks.js.

    0 讨论(0)
提交回复
热议问题