javascript - 类型错误 : module is not a function AngularJS & Jasmine

标签 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>

收藏夹 Controller :

  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: module is not a function

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

最佳答案

你需要在 Jasmine 之后包含 angular-mocks.js 否则像 moduleinject 这样的功能将不会被定义。

此外你重新定义了module:

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

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

关于javascript - 类型错误 : module is not a function AngularJS & Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32274062/

相关文章:

javascript - 具有复制到剪贴板纯 JS 的自定义选择功能

javascript - 如何在多维数组上使用FILTER来检索唯一值?

javascript - 为什么 ko.observableArray.length 不返回底层数组的长度?

javascript - 如何在 Jasmine 中编写 FileReader 测试?

javascript - 这是否违反了任何测试原则?

javascript - 防止模态框上的背景滚动

javascript - 如何让 ng-disabled 检查 ng-repeat 中的项目值(使用 AngularJS)

javascript - AngularJs 将模型值+模型名称传递给 Controller ​​中的函数

javascript - AngularJS 种子中的多个 ng-App 代码段是什么意思?

javascript - 如何使用 Jasmine/Chutzpah/JsTestDriver 对 JavaScript 即时函数进行单元测试