[英]Unit testing angular controller
我有一个名为“小部件”的角度模块。 这是我的应用程序中正在使用的签名:
var app = angular.module('widgets', [
'widget.Panel',
'widget.List',
'services'
]);
我也有一个从app
创建的控制器:
app.controller('clientListController', ['$scope', '$http', 'ServiceProvider', function ($scope, $http, ServiceProvider) {
我正在尝试为“ clientListController”编写单元测试。 这是我所做的:
describe('clientListController', function () {
var ctrl, scope;
beforeEach(angular.module('widgets'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('clientListController', { $scope: scope });
}));
it("should define openClient function", function () {
expect(scope.openClient).toBeDefined();
});
});
当我尝试运行测试时,出现错误:
Error: Argument 'clientListController' is not a function, got undefined
我编写测试的方式有什么问题吗? 为什么没有定义clientListController
? 它与依赖关系有关吗?
使用beforeEach(module('widgets'))
或beforeEach(angular.mock.module('widgets'))
。 您需要设置一个模块,以使角-工作。 为了方便起见,将angular.mock.module
分配给window.module
。 您的代码只是从角度获取模块,而没有将其设置以供角度模拟使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.