如何使用mocha模拟eventHubclient (azure/event-hub)生产者连接进行单元测试?<



下面是节点js代码为了调用API调用,我想模拟eventHub连接。


const { EventHubProducerClient } = require("@azure/event-hubs");
const connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING";
const eventHubName = "EVENT HUB NAME";
async function main() {
// Create a producer client to send messages to the event hub.
const producer = new EventHubProducerClient(connectionString, eventHubName);
// Prepare a batch of three events.
const batch = await producer.createBatch();
batch.tryAdd({ body: "First event" });
batch.tryAdd({ body: "Second event" });
batch.tryAdd({ body: "Third event" });    
// Send the batch to the event hub.
await producer.sendBatch(batch);
// Close the producer client.
await producer.close();
console.log("A batch of three events have been sent to the event hub");
}
main().catch((err) => {
console.log("Error occurred: ", err);
});

请帮忙,谢谢。

我将使用sinon作为存根库。它经常和摩卡一起使用。来自文档How to stub a dependency of a module:

Sinon是一个存根库,而不是模块拦截库。存根依赖项高度依赖于您的环境和实现。对于Node环境,我们通常推荐针对链接接缝或显式依赖注入的解决方案。尽管在一些更基本的情况下,您可以通过修改依赖项的模块导出来只使用Sinon。

要存根测试中模块的依赖项(导入的模块),您必须在测试中显式地导入它并存根所需的方法。要使存根工作,存根方法不能被解构,既不在被测模块中也不在被测模块中。

因为被测代码的依赖是从包中导入的类,而不是使用依赖注入。我将使用链接接缝来存根@azure/event-hubs模块和EventHubProducerClient类。

main.js:

const { EventHubProducerClient } = require('@azure/event-hubs');
const connectionString = 'EVENT HUBS NAMESPACE CONNECTION STRING';
const eventHubName = 'EVENT HUB NAME';
async function main() {
// Create a producer client to send messages to the event hub.
const producer = new EventHubProducerClient(connectionString, eventHubName);
// Prepare a batch of three events.
const batch = await producer.createBatch();
batch.tryAdd({ body: 'First event' });
batch.tryAdd({ body: 'Second event' });
batch.tryAdd({ body: 'Third event' });
// Send the batch to the event hub.
await producer.sendBatch(batch);
// Close the producer client.
await producer.close();
console.log('A batch of three events have been sent to the event hub');
}
module.exports = main;

main.test.js:

const proxyquire = require('proxyquire');
const sinon = require('sinon');
describe('68808576', () => {
it('should pass', async () => {
const batchStub = {
tryAdd: sinon.stub(),
};
const producerStub = {
createBatch: sinon.stub().resolves(batchStub),
sendBatch: sinon.stub(),
close: sinon.stub(),
};
const EventHubProducerClientStub = sinon.stub().returns(producerStub);
const main = proxyquire('./main', {
'@azure/event-hubs': { EventHubProducerClient: EventHubProducerClientStub },
});
await main();
sinon.assert.calledWithExactly(
EventHubProducerClientStub,
'EVENT HUBS NAMESPACE CONNECTION STRING',
'EVENT HUB NAME',
);
sinon.assert.calledOnce(producerStub.createBatch);
sinon.assert.calledThrice(batchStub.tryAdd);
sinon.assert.calledWithExactly(producerStub.sendBatch, batchStub);
sinon.assert.calledOnce(producerStub.close);
});
});

测试结果:

68808576
A batch of three events have been sent to the event hub
✓ should pass (1946ms)

1 passing (2s)
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
main.js  |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

最新更新