如何使用返回的多个链接函数对Jasmine进行单元测试? [英] How to unit test with Jasmine on multiple chained functions with returns?

查看:126
本文介绍了如何使用返回的多个链接函数对Jasmine进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

/**
* filters array down to the given allowed keys
* @param {Object} data
* @param {String[]} allowedKeys
*/
$scope.filterData = function(data, allowedKeys) {
    return Object.keys(data)
        .filter(function(key) {
            return allowedKeys.includes(key);
        })
        .reduce(function(obj, key) {
            obj[key] = data[key];
            return obj;
        }, {});
};

我想创建一个单元测试,到目前为止,我有以下内容:

that I want to create a unit test for and so far I have the following:

describe('$scope.filterData', function() { 
        //params
        var data = {
            key1: "value1",
            key2: "value2",
            key3: "value3"
        }
        var allowedKeys = ["key1", "key2"];
        //mockobject
        var $Object = jasmine.createSpyObj('Object', ['keys', 'filter', 'reduce']);

        it('should func', function() {

            $Object.keys.and.returnValue($Object);
            $Object.filter.and.returnValue($Object);
            $Object.reduce.and.returnValue($Object);

            $scope.filterData(data, allowedKeys);
            expect(Object.filter).toHaveBeenCalled();
        });
    });

我遇到的问题是我收到以下错误:

The issue that I am having, is that I am getting the following error:


TypeError:undefined不是构造函数(评估'allowedKeys.includes(key)')

TypeError: undefined is not a constructor (evaluating 'allowedKeys.includes(key)')

我不明白,如何解决这个错误?

I do not understand, how to fix that error?

推荐答案

首先,对象没有函数 filter 而你的 jasmine.createSpyObj 是字面上没用。正如@Luan Phan在他的回答中所说,我们通常倾向于测试函数的输入和输出值。 Javascript内置函数不需要在我们的测试中进行测试。

First of all, Object does not have a function filterand your jasmine.createSpyObj is literally useless. As @Luan Phan says in his answer, we usually tend to test the input and output value for the function. Javascript built-in functions do not need to be tested in our tests.

但是,如果你想知道,例如,如果对象.keys 在您的函数内部被调用,这是一个示例

But, if you would like to know, for example, if Object.keys was called inside your function, here is an example

it('should func', function () {                        
    spyOn(Object, 'keys').and.callThrough();

    $scope.filterData(data, allowedKeys);                     

    expect(Object.keys).toHaveBeenCalled();
});

对于 filterData

it('should func', function () {
    spyOn(Object, 'keys').and.callThrough();
    spyOn(Array.prototype, 'filter').and.callThrough();
    spyOn(Array.prototype, 'reduce').and.callThrough();

    $scope.filterData(data, allowedKeys);

    expect(Object.keys).toHaveBeenCalled();
    expect(Array.prototype.filter).toHaveBeenCalled();
    expect(Array.prototype.reduce).toHaveBeenCalled();
});

如果你真的需要模拟其中一个内置函数返回,这是一个例子

If you really need to mock what one of the built-in functions returns, here is an example

it('should func', function () {
    const mockResult = {};

    spyOn(Array.prototype, 'reduce').and.returnValue(mockResult);

    const result = filterData(data, allowedKeys);

    expect(result).toBe(mockResult);
});

同样,Javascript的内置函数已经在其他地方编写了测试,我们不需要在测试中测试它,我们的重点应放在我们编写的函数上。

Again, Javascript's built-in functions already have tests written in some other place, we don't need to test it in our test, our focus should be on the functions we write.

希望有帮助

这篇关于如何使用返回的多个链接函数对Jasmine进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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