Javascript 类型错误:模块不是 AngularJS 和 Jasmine 的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32274062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:49:45  来源:igfitidea点击:

TypeError: module is not a function AngularJS & Jasmine

javascriptangularjsjasmine

提问by Teoman shipahi

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

收藏夹Controller.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?

运行测试后出错。我错过了什么吗?

回答by a better oliver

You need to include angular-mocks.jsafterJasmine otherwise functions like moduleor injectwill not be defined.

您需要angular-mocks.jsJasmine之后包含否则类似moduleinject不会定义功能。

Moreover you redefine module:

此外,您重新定义module

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

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

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

回答by Sergio Esteban

Try this line in the tag body:

在标签正文中尝试这一行:

<body ng-app = 'AngularSampleApp'>