Testing service in Angular returns module is not defined

后端 未结 7 1184
借酒劲吻你
借酒劲吻你 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: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.

提交回复
热议问题