本文介绍了开玩笑createSpyObj的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Chai,您可以创建一个间谍对象,如下所示:

With Chai, you can create a spy object as follows:

chai.spy.object([ 'push', 'pop' ]);

使用茉莉花,您可以使用:

With jasmine, you can use:

jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);

什么叫开玩笑?

上下文:我目前正在将(打字稿)Jasmine测试迁移到(打字稿)Jest.在这种情况下,迁移指南基本上没有用: https://facebook.github. io/jest/docs/migration-guide.html 与任何相对较新的技术一样,在文档中找不到与此相关的任何内容.

Context: I am currently migrating a (typescript) Jasmine tests to (typescript) Jest. The migration guide is basically useless in this case: https://facebook.github.io/jest/docs/migration-guide.html As with any relatively new tech, there's nothing that can easily be found in the docs about this.

推荐答案

我为开玩笑编写了一个非常快速的createSpyObj函数,以支持旧项目.基本上从Jasmine的实现移植而来.

I've written a very quick createSpyObj function for jest, to support the old project. Basically ported from Jasmine's implementation.

export const createSpyObj = (baseName, methodNames): { [key: string]: Mock<any> } => {
    let obj: any = {};

    for (let i = 0; i < methodNames.length; i++) {
        obj[methodNames[i]] = jest.fn();
    }

    return obj;
};

这篇关于开玩笑createSpyObj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 10:58