最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: module is not a function AngularJS & Jasmine - Stack Overflow

programmeradmin1浏览0评论

In my sample app have I test runner like this

  <!DOCTYPE html>
<html xmlns="">
<head>
    <title></title>
</head>
<body>
    <!--angular-->
    <script src="../../../Scripts/angular.min.js"></script>
    <!--jasmine-->
    <img src="../../../Content/jasmine/jasmine_favicon.png" />
    <link href="../../../Content/jasmine/jasmine.css" rel="stylesheet" />
    <script src="../../../Scripts/jasmine/jasmine.js"></script>
    <script src="../../../Scripts/jasmine/jasmine-html.js"></script>
    <script src="../../../Scripts/jasmine/boot.js"></script>
    <!--angular mocks-->
    <script src="../../../Scripts/angular-mocks.js"></script>
    <!--app tests-->
    <script src="../../FavoritesController.js"></script>
    <script src="FavoritesController.Tests.js"></script>
</body>
</html>

FavoritesController:

  var module = angular.module('AngularSampleApp', []);
var FavoritesController = module.controller('FavoritesController', function favoritesController($scope) {
    $scope.phones = [
        {
            'name': 'Nexus S',
            'snippet': 'Fast just got faster with Nexus S.'
        },
        {
            'name': 'Motorola XOOM™ with Wi-Fi',
            'snippet': 'The Next, Next Generation tablet.'
        },
        {
            'name': 'MOTOROLA XOOM™',
            'snippet': 'The Next, Next Generation tablet.'
        }
    ];

});

FavoritesController.Tests.js

describe('FavoritesController', function () {
    beforeEach(module('AngularSampleApp'));
    it('should create "phones" model with 3 phones', inject(function ($controller) {
        var scope = {},
            ctrl = $controller('FavoritesController', { $scope: scope });

        expect(scope.phones.length).toBe(3);
    }));
});

But I am getting:

TypeError: module is not a function

error after I run my tests. Am I missing something?

In my sample app have I test runner like this

  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <!--angular-->
    <script src="../../../Scripts/angular.min.js"></script>
    <!--jasmine-->
    <img src="../../../Content/jasmine/jasmine_favicon.png" />
    <link href="../../../Content/jasmine/jasmine.css" rel="stylesheet" />
    <script src="../../../Scripts/jasmine/jasmine.js"></script>
    <script src="../../../Scripts/jasmine/jasmine-html.js"></script>
    <script src="../../../Scripts/jasmine/boot.js"></script>
    <!--angular mocks-->
    <script src="../../../Scripts/angular-mocks.js"></script>
    <!--app tests-->
    <script src="../../FavoritesController.js"></script>
    <script src="FavoritesController.Tests.js"></script>
</body>
</html>

FavoritesController:

  var module = angular.module('AngularSampleApp', []);
var FavoritesController = module.controller('FavoritesController', function favoritesController($scope) {
    $scope.phones = [
        {
            'name': 'Nexus S',
            'snippet': 'Fast just got faster with Nexus S.'
        },
        {
            'name': 'Motorola XOOM™ with Wi-Fi',
            'snippet': 'The Next, Next Generation tablet.'
        },
        {
            'name': 'MOTOROLA XOOM™',
            'snippet': 'The Next, Next Generation tablet.'
        }
    ];

});

FavoritesController.Tests.js

describe('FavoritesController', function () {
    beforeEach(module('AngularSampleApp'));
    it('should create "phones" model with 3 phones', inject(function ($controller) {
        var scope = {},
            ctrl = $controller('FavoritesController', { $scope: scope });

        expect(scope.phones.length).toBe(3);
    }));
});

But I am getting:

TypeError: module is not a function

error after I run my tests. Am I missing something?

Share Improve this question edited Aug 28, 2015 at 15:59 Teoman shipahi asked Aug 28, 2015 at 15:00 Teoman shipahiTeoman shipahi 23.1k16 gold badges144 silver badges172 bronze badges 8
  • 1 You would need to include angular.mocks, cdnjs.cloudflare.com/ajax/libs/angular.js/1.x.x/… where x.x is the version of angular you are using. – PSL Commented Aug 28, 2015 at 15:06
  • 1 @PSL thanks, I updated my question and I am getting same error. Still. – Teoman shipahi Commented Aug 28, 2015 at 15:07
  • 1 Perhaps something is overwriting the module on the window object? did you try using angular.mock.module? Did you try doing a console.log to see what is module? – PSL Commented Aug 28, 2015 at 15:08
  • 2 angular.mock looks like defined, when I do console.log(angular.mock.module) comes as undefined. weird. – Teoman shipahi Commented Aug 28, 2015 at 15:14
  • 1 Yeah i guess you arent using latest version of angular. What does module give you though? – PSL Commented Aug 28, 2015 at 15:15
 |  Show 3 more comments

2 Answers 2

Reset to default 16

You need to include angular-mocks.js after Jasmine otherwise functions like module or inject will not be defined.

Moreover you redefine module:

var module = angular.module('AngularSampleApp', []);

So either you rename the variable or put the code inside an IIFE.

Try this line in the tag body:

<body ng-app = 'AngularSampleApp'>
发布评论

评论列表(0)

  1. 暂无评论