【发布时间】:2015-11-23 07:02:04
【问题描述】:
在我的示例应用中,我有这样的测试跑步者
<!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: 模块不是函数
运行测试后出错。我错过了什么吗?
【问题讨论】:
-
您需要包含 angular.mocks, cdnjs.cloudflare.com/ajax/libs/angular.js/1.x.x/… 其中 x.x 是您正在使用的 angular 版本。
-
@PSL 谢谢,我更新了我的问题,我得到了同样的错误。仍然。
-
也许有些东西正在覆盖窗口对象上的模块?您是否尝试使用 angular.mock.module?你有没有尝试做一个 console.log 来看看什么是模块?
-
angular.mock 看起来像已定义,当我执行 console.log(angular.mock.module) 时未定义。很奇怪。
-
是的,我猜你没有使用最新版本的 Angular。模块能给你什么?
标签: javascript angularjs jasmine