ReganPerkins
ReganPerkins

Reputation: 1665

Unknown Provider when unit testing filter

I am unit testing my first filter, which seems easy enough but I keep getting

Unknown provider: activeProfileProvider <- activeProfile <- pbRoles <- rolesFilter

The activeProfile is a consent that is a dependency of pbRoles. My understanding of constants is limited and I cant seem to find a way to include it. I have tried adding the config file where the constants are declared as well as mocking the constant in the test to no avail.

does anyone know if there is a specific way to do this? or does my problem lay else where?

My filter:

angular.module('pb.roles.filters')
    .filter('roles', ['pbRoles', function (pbRoles) {
        return function (input) {
            if (!input) {
                return "None";
            } else {
                return pbRoles.roleDisplayName(input);
            }
        };

    }]);

My test:

describe('RolesController', function () {

    beforeEach(module('pb.roles'));
    beforeEach(module('pb.roles.filters'));
    beforeEach(module('ui.router'));
    beforeEach(module('ui.bootstrap'));

    var rolesFilter;
    var mockActiveProfile = {};

    beforeEach(inject(function (_rolesFilter_) {
        activeProfile = mockActiveProfile;
        rolesFilter = _rolesFilter_;
    }));

    var pbRoles = {
        roleDisplayName: function (input) {
            return input
        }
    };

    describe('role: filter', function () {

        it('should return None if called with no input', function () {
            expect(rolesFilter()).toBe('None');
        });

        it('should call roleDisplayName with input', function () {
            expect(roles('Hello')).toBe('Hello');
        });
    });

});

have also tried mocking the constent like so:

 module(function ($provide) {
        $provide.constant('activeProfile', function () {
            pbGlobal.activeProfile = {
                profile: ""
            }
        });
    });

Upvotes: 0

Views: 666

Answers (1)

ReganPerkins
ReganPerkins

Reputation: 1665

mocking the providers at the top of the page like so worked:

beforeEach(module(function ($provide) {
        $provide.constant('organizationService', function () {

        });
        $provide.service('activeProfile', function () {
            activeProfile = {
                profile: ""
            }
        });
    }));

http://www.sitepoint.com/mocking-dependencies-angularjs-tests/ was a huge help.

My full working test if anyone is curious:

describe('RolesController', function () {

    var mockPbRoles = {
        roleDisplayName: function (input) {
            return "it worked!"
        }
    };

    beforeEach(module(function ($provide) {
        $provide.value('pbRoles', mockPbRoles);
        $provide.constant('organizationService', function () {});
        $provide.service('activeProfile', function () { });
    }));

    beforeEach(module('pb.roles'));
    beforeEach(module('pb.roles.filters'));
    beforeEach(module('ui.router'));
    beforeEach(module('ui.bootstrap'));

    var rolesFilter;

    beforeEach(inject(function (_rolesFilter_) {
        rolesFilter = _rolesFilter_;
    }));


    describe('role: filter', function () {

        it('should return None if called with no input', function () {
            expect(rolesFilter(false)).toBe('None');
        });

        it('should call roleDisplayName with input', function () {
            result = rolesFilter(true);
            expect(result).toEqual("it worked!");
        });
    });

}); 

Upvotes: 1

Related Questions