TypeError: module is not a function AngularJS & Jasmine
TypeError: module is not a function AngularJS & Jasmine
在我的示例应用程序中,我是否像这样测试 运行ner
<!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>
收藏夹控制器:
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
我 运行 测试后出错。我错过了什么吗?
您需要在 angular-mocks.js
之后包含 Jasmine,否则将无法定义 module
或 inject
等函数。
而且你重新定义module
:
var module = angular.module('AngularSampleApp', []);
因此,要么重命名变量,要么将代码放入 IIFE 中。
在标签正文中试试这一行:
<body ng-app = 'AngularSampleApp'>
在我的示例应用程序中,我是否像这样测试 运行ner
<!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>
收藏夹控制器:
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
我 运行 测试后出错。我错过了什么吗?
您需要在 angular-mocks.js
之后包含 Jasmine,否则将无法定义 module
或 inject
等函数。
而且你重新定义module
:
var module = angular.module('AngularSampleApp', []);
因此,要么重命名变量,要么将代码放入 IIFE 中。
在标签正文中试试这一行:
<body ng-app = 'AngularSampleApp'>