如何使用Karma和Jasmine在角度服务中测试“私有”功能 [英] How to test 'private' functions in an angular service with Karma and Jasmine

查看:142
本文介绍了如何使用Karma和Jasmine在角度服务中测试“私有”功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的角应用程序中有一个服务,看起来像这样:

I have a service in my angular app that looks something like this:

angular.module('BracketService', []).factory('BracketService', [function() {
    function compareByWeight(a, b) {
        return a.weight - b.weight;
    }
    function filterWeightGroup(competitors, lowWeight, highWeight) {
        //filter stuff
    }
    function createBracketsByWeightGroup(weightGroup) {
        //create some brackets
    }
    //set some base line values
    var SUPER_HEAVY_WEIGHT = 500;
    var SUPER_LIGHT_WEIGHT = 20;
    return {
        //create brackets from a list of competitors
        returnBrackets: function(competitors) {
            var brackets = {};
            //get super light weights
            brackets.superLightWeights = createBracketsByWeightGroup(
                filterWeightGroup(competitors, 0, SUPER_LIGHT_WEIGHT)
                .sort(compareByWeight)
            );
            brackets.superHeavyWeights = createBracketsByWeightGroup(
                filterWeightGroup(competitors, SUPER_HEAVY_WEIGHT, Infinity)
                .sort(compareByWeight)
            );
            brackets.middleWeights = createBracketsByWeightGroup(
                filterWeightGroup(competitors, SUPER_LIGHT_WEIGHT, SUPER_HEAVY_WEIGHT)
                .sort(compareByWeight)
            );
            return brackets;
        }
    };

}]);

我想单元测试不仅仅是在return语句中公开的函数/属性,而是还有返回语句之外的函数。

I would like to unit test not just the functions / properties that are exposed in the return statement, but also the functions that are outside of the return statement.

我的测试目前设置如下:

My test is currently set up something like this:

describe('BracketService', function() {
    beforeEach(module('bracketManager'));

    it('calling return brackets with no competitors will return 3 empty weight classes', inject(function(BracketService) {
        var mockCompetitors = [];
        var mockBracketResult = {superHeavyWeights: [[]], superLightWeights: [[]], middleWeights: [[]]};
        expect(BracketService.returnBrackets(mockCompetitors)).toEqual(mockBracketResult);
    }));
});

但是如何测试return语句未公开的compare,filter和createBrackets函数?

But how do I test the compare, filter and createBrackets functions that are not exposed by the return statement?

谢谢!

推荐答案

无法测试这些功能。它们的范围是包含BracketService工厂的功能,它们在任何其他地方都是不可见的。如果你想测试它们,那么你必须以某种方式暴露它们。

There is no way to test those functions. Their scope is the function that comprises your BracketService factory and they are invisible anyplace else. If you want to test them, then you have to expose them somehow.

你可以将它们移动到自己的服务中(这看起来有点矫枉过正)或者你可以进行黑盒测试您的BracketService服务具有足够的数据组合,以确保内部功能正常工作。这可能是最明智的方法。

You can move them into their own service (which seems like overkill) or you can black box test your BracketService service with enough data combinations to make sure the internal functions are working. That's probably the most sensible approach.

如果您不想将它们放在单独的服务中,但仍然觉得需要测试这些内部函数,只需返回它们即可从工厂和returnBrackets。

If you don't want to put them in a separate service, but still feel the need to test those internal functions, just return them from the factory along with returnBrackets.

当我有一些直接单独测试的辅助函数时,我可能会这样做,但打开一个组合潘多拉的盒子进行黑盒测试。我通常使用_来表示这些函数,以显示它们是辅助函数,并且仅在测试时公开。

I might do this when I have a number of helper functions that are straight forward to test individually, but open up a combinatorial Pandora's box to black box test. I usually preface such functions with an "_" to show they are helper functions and are only exposed for testing.

return {
    //create brackets from a list of competitors
    returnBrackets: function(competitors) {...},
    _filterWeightGroup: filterWeightGroup,
    _createBracketsByWeightGroup: createBracketsByWeightGroup
   };

这篇关于如何使用Karma和Jasmine在角度服务中测试“私有”功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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