mardi 16 juin 2015

Angular Karma Unit Testing Controller Not Injecting Mocked Service Dependency

I have tried various ways of instantiating the mocked service 'listFilterManager' and passing this to the controller. Although the mocked service is created and accessible, when the scope.$digest() is executed, the actual non-mocked service runs instead of the mocked listFilterManager. Even more confusing is that the resourceStore service is properly mocked and injected. Any help would be greatly appreciated.

Relevant code:

    beforeEach(function(){
        module('listModule');
        module(function($provide){
            $provide.factory('resourceStore', function(){
                return {
                    alertsArray: Object.keys(mockData).map(function(key){
                        return mockData[key];
                    })
                };
            });
            $provide.value('listFilterManager',{
                filterList: jasmine.createSpy('filterList').and.callFake(function(input){
                    return input;
                })
            });
        });
    });

    describe('instantiates the Alert List Ctrl', function(){
        var scope, listFilterParams, listFilterManager, resourceStore, tableAccessorDictionaries, AlertListCtrl;
        beforeEach(function(){
            inject(function(_$rootScope_, $controller, _tableAccessorDictionaries_, _listFilterParams_, _resourceStore_, _listFilterManager_){
                scope = _$rootScope_.$new();
                listFilterParams = _listFilterParams_;
                listFilterManager = _listFilterManager_; //this refs the mocked service, but the actual service is the one that is executed.
                var deps = {
                    '$scope': scope,
                    'tableAccessorDictionaries': _tableAccessorDictionaries_,
                    'listFilterManager': _listFilterManager_,
                    'listFilterParams': _listFilterParams_,
                    'resourceStore': _resourceStore_
                };
                AlertListCtrl = $controller('AlertListCtrl', deps);
            });
        });

        it('it should filter the list when a list filter parameter changes', function(){
            expect(listFilterManager.filterList).not.toHaveBeenCalled();
            scope.filterParams.text = 'This is a test query';
            scope.$digest();
            expect(listFilterManager.filterList).toHaveBeenCalled();
        });

    });

Aucun commentaire:

Enregistrer un commentaire