单元测试过滤器时未知的提供程序 [英] Unknown Provider when unit testing filter

查看:82
本文介绍了单元测试过滤器时未知的提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对第一个过滤器进行单元测试,这似乎很简单,但我一直在获得

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

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

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

activeProfile是一个同意,它是pbRoles的依赖项.我对常量的理解是有限的,而且我似乎找不到找到将其包含在内的方法.我试过在声明常量的地方添加配置文件,以及在测试中嘲笑常量都无济于事.

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?

我的过滤器:

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

});

还尝试过像这样嘲笑该常量:

have also tried mocking the constent like so:

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

推荐答案

在页面顶部模拟提供程序,就像这样:

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 /是一个巨大的帮助.

如果有人好奇,我会进行全面的测试:

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

}); 

这篇关于单元测试过滤器时未知的提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆