TypeError:模块不是AngularJS&茉莉花

时间:2015-08-28 15:00:13

标签: javascript angularjs jasmine

在我的示例应用中,我测试了这样的跑步者

  <!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);
    }));
});

但我得到了:

  

TypeError:模块不是函数

运行测试后出现

错误。我错过了什么吗?

2 个答案:

答案 0 :(得分:14)

您需要在 之后添加angular-mocks.js ,否则将无法定义moduleinject等函数。

此外,您重新定义module

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

因此,您要么重命名变量,要么将代码放在IIFE中。

答案 1 :(得分:-3)

在标签正文中尝试此行:

<body ng-app = 'AngularSampleApp'>