单元测试过滤器时的未知提供商

时间:2015-02-25 18:37:27

标签: angularjs unit-testing jasmine

我正在对我的第一个过滤器进行单元测试,这似乎很容易,但我一直在

  

未知提供者:activeProfileProvider< - activeProfile< - pbRoles< - rolesFilter

activeProfile是同意,是pbRoles的依赖。我对常数的理解是有限的,我似乎无法找到一种方法来包含它。我已经尝试添加配置文件,其中声明常量以及模拟测试中的常量无济于事。

有没有人知道是否有特定方法可以做到这一点?或者问题出在哪里?

我的过滤器:

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

    }]);

我的测试:

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

});

也试过嘲笑这样的意思:

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

1 个答案:

答案 0 :(得分:1)

模仿页面顶部的提供程序就像这样工作:

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

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

http://www.sitepoint.com/mocking-dependencies-angularjs-tests/是一个巨大的帮助。

如果有人好奇,我的全面工作测试:

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!");
        });
    });

});