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

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

问题描述

我的 Angular 应用中有一项服务,如下所示:

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 语句中公开的函数/属性进行单元测试,还要对 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 在 Angular 服务中测试“私有"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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