Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Jasmine中测试Typescript函数?_Typescript_Unit Testing_Jasmine - Fatal编程技术网

如何在Jasmine中测试Typescript函数?

如何在Jasmine中测试Typescript函数?,typescript,unit-testing,jasmine,Typescript,Unit Testing,Jasmine,这是一个直截了当的问题,但我找不到答案。Jasmine文档可以改进。它主要展示了监视模拟数据和函数的示例,但我想测试我编写的函数。否则,我实际测试的是什么?当我使用管道时,我发现它更直接,因为我所要做的就是用new关键字实例化管道。然后断言它会返回我期望的结果。 e、 g 但是,这些函数似乎不起作用。以下面的代码为例: export function getKeyValueOfAttributesFromElement(el) { const attributeMap = {};

这是一个直截了当的问题,但我找不到答案。Jasmine文档可以改进。它主要展示了监视模拟数据和函数的示例,但我想测试我编写的函数。否则,我实际测试的是什么?当我使用管道时,我发现它更直接,因为我所要做的就是用new关键字实例化管道。然后断言它会返回我期望的结果。 e、 g

但是,这些函数似乎不起作用。以下面的代码为例:

export function getKeyValueOfAttributesFromElement(el) {
    const attributeMap = {};
    Object.keys(el.attributes)
        .forEach(key => {
            attributeMap[el.attributes[key].nodeName] = el.attributes[key].value;
        });

    return attributeMap;
};
//我的测试

import { getKeyValueOfAttributesFromElement } from './dom';


describe('getKeyValueOfAttributesFromElement', () => {
    let mockData;

    beforeEach(() => {
        mockData = {
            attributes: {
                a: 'somestring',
                b: 42,
                c: false
            }
        };
    });

    it('should work', () => {

        const myFunc = getKeyValueOfAttributesFromElement(mockData);

        expect(myFunc).toHaveBeenCalled();
    });
});
在我的浏览器中,我没有传递这个消息,而是得到了以下错误
应该是间谍,但是得到了({undefined:undefined})。

如果我使用
new
关键字调用函数
只能使用'new'关键字调用void函数。


我错过了什么?我怎样才能在测试中调用我的函数而不必编写一个函数呢?

嗯,
getKeyValueOfAttributesFromElement
不是间谍,这是Jasmine说的
TohaveEncalled
方法只能应用于Jasmine间谍对象
myFunc
是执行的结果,但不是间谍

您必须先将
getKeyValueOfAttributesFromElement
函数存根,但创建独立导出函数()的间谍非常棘手


我建议根本不要勾选
来填充
,因为这没有任何意义。您通常使用
对依赖项进行调用
来单独测试您的函数,而不是对您刚刚调用的原始函数进行测试。只需针对不同的数据集测试结果,在这种情况下,这将是100%正确的方法。

茉莉花错误可能具有欺骗性。显然,问题出在
.tohavebeencall()
方法上

当我更新mock并使用
.toEqual()
时,它通过了

describe('getKeyValueOfAttributesFromElement', () => {
let mockData;
it('should return null', () => {
    mockData = {
        attributes: {
            a: {
                nodename: 'something',
                value: 'a value'
            }
        }
    };

    expect(getKeyValueOfAttributesFromElement(mockData)).not.toBeNull();

});

it('should return attributes', () => {
    mockData = {
        attributes: {
            a: {
                nodename: 'something',
                value: 'a value'
            }
        }
    };
    expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': 'a value'});

});

it('should return invalid output', () => {
    mockData = {
        attributes: {
            a: {
                nodename: 'something',
                value: 'a value'
            },
            b: {
                name: false,
                value: ''
            }
        }
    };

    expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': ''});


});
});

错误::getKeyValueOfAttributesFromElement未声明为可写或没有setter@London804你说得对,检查我的最新答案,谢谢
describe('getKeyValueOfAttributesFromElement', () => {
let mockData;
it('should return null', () => {
    mockData = {
        attributes: {
            a: {
                nodename: 'something',
                value: 'a value'
            }
        }
    };

    expect(getKeyValueOfAttributesFromElement(mockData)).not.toBeNull();

});

it('should return attributes', () => {
    mockData = {
        attributes: {
            a: {
                nodename: 'something',
                value: 'a value'
            }
        }
    };
    expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': 'a value'});

});

it('should return invalid output', () => {
    mockData = {
        attributes: {
            a: {
                nodename: 'something',
                value: 'a value'
            },
            b: {
                name: false,
                value: ''
            }
        }
    };

    expect(getKeyValueOfAttributesFromElement(mockData)).toEqual({'undefined': ''});


});
});