如何在测试中的另一个模块调用的模块中存根/模拟一个函数?

时间:2021-12-28 00:41:38

I am trying to test a function in module X. It is a fairly complex function. In this function, I import a function in Module Y that calls a third party service. How can I stub this 3rd party service in Module Y if my tests for module X don't import module Y?

我试图在模块X中测试一个函数。这是一个相当复杂的函数。在此函数中,我在模块Y中导入一个调用第三方服务的函数。如果模块X的测试不导入模块Y,如何在模块Y中存根此第三方服务?

2 个解决方案

#1


0  

You can patch along the require seam with a project like mockery:

您可以使用像mockery这样的项目沿需求接缝进行修补:

If you've tried working with mocks in Node.js, you've no doubt discovered that it's not so easy to get your mocks hooked up in the face of Node's module loading system. When your source-under-test pulls in its dependencies through require, you want your mocks provided, instead of the original module, to enable true unit testing of your code.

如果您尝试过在Node.js中使用模拟,那么毫无疑问,您会发现在面对Node的模块加载系统时,让您的模拟更加容易。当您的测试源通过require拉入其依赖项时,您希望提供的模拟(而不是原始模块)能够对代码进行真正的单元测试。

https://github.com/mfncooper/mockery

https://github.com/mfncooper/mockery

#2


0  

Let's say your module X returns the result of an operation performed by Y, we can say that X is as follows:

假设您的模块X返回Y执行的操作的结果,我们可以说X如下:

const y = require ('y.js');

xWork () {
    return y.doWork().result;
}

Here you can update X to inject Y like this:

在这里你可以像这样更新X注入Y:

xWork (y) {
    return y.doWork().result;
}

From there you in your test you can generate a mock of Y that can be injected like this:

从那里你在测试中你可以生成一个Y的模拟,可以像这样注入:

yMock = {
    doWork: () => { 
        return { result: 'mockedResult' };
    }
};

x.xWork(yMock); // call x with y mock

Javascript allows you to mock anything without requiring any dependency which is perfect for unit testing. Hope it helps.

Javascript允许您模拟任何东西,而不需要任何依赖,这是单元测试的理想选择。希望能帮助到你。

#1


0  

You can patch along the require seam with a project like mockery:

您可以使用像mockery这样的项目沿需求接缝进行修补:

If you've tried working with mocks in Node.js, you've no doubt discovered that it's not so easy to get your mocks hooked up in the face of Node's module loading system. When your source-under-test pulls in its dependencies through require, you want your mocks provided, instead of the original module, to enable true unit testing of your code.

如果您尝试过在Node.js中使用模拟,那么毫无疑问,您会发现在面对Node的模块加载系统时,让您的模拟更加容易。当您的测试源通过require拉入其依赖项时,您希望提供的模拟(而不是原始模块)能够对代码进行真正的单元测试。

https://github.com/mfncooper/mockery

https://github.com/mfncooper/mockery

#2


0  

Let's say your module X returns the result of an operation performed by Y, we can say that X is as follows:

假设您的模块X返回Y执行的操作的结果,我们可以说X如下:

const y = require ('y.js');

xWork () {
    return y.doWork().result;
}

Here you can update X to inject Y like this:

在这里你可以像这样更新X注入Y:

xWork (y) {
    return y.doWork().result;
}

From there you in your test you can generate a mock of Y that can be injected like this:

从那里你在测试中你可以生成一个Y的模拟,可以像这样注入:

yMock = {
    doWork: () => { 
        return { result: 'mockedResult' };
    }
};

x.xWork(yMock); // call x with y mock

Javascript allows you to mock anything without requiring any dependency which is perfect for unit testing. Hope it helps.

Javascript允许您模拟任何东西,而不需要任何依赖,这是单元测试的理想选择。希望能帮助到你。