简体   繁体   中英

Unit testing angular controller

I have an angular module called 'widgets'. Here is it's signature being used in my app:

var app = angular.module('widgets', [
    'widget.Panel',
    'widget.List',
    'services'
]);

I also have a controller being created off of app :

app.controller('clientListController', ['$scope', '$http', 'ServiceProvider', function ($scope, $http, ServiceProvider) {

I'm trying to write a unit test for 'clientListController'. Here is what I've done:

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();
    });
});

When I try to run the tests, I get an error:

Error: Argument 'clientListController' is not a function, got undefined 

Is there something wrong with how I've written the test? How come clientListController isn't defined? Does it have something to do with the dependencies?

Use beforeEach(module('widgets')) or beforeEach(angular.mock.module('widgets')) . You need to set a module for angular-mocks to work from. angular.mock.module gets assigned to window.module for convenience. Your code is just getting the module from angular and not setting it for angular-mocks to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM