[英]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.