单元测试角度控制器

时间:2014-02-18 18:37:47

标签: javascript angularjs unit-testing

我有一个名为'小部件的角度模块'。这是我的应用程序中使用的签名:

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没有定义?它与依赖项有关吗?

1 个答案:

答案 0 :(得分:1)

使用beforeEach(module('widgets'))beforeEach(angular.mock.module('widgets'))。你需要设置一个模块来进行角度模拟。为方便起见,angular.mock.module被分配到window.module。你的代码只是从角度获取模块,而不是设置角度模拟使用。