简体   繁体   English

使用 mocha 在 node.js 中编写单元测试用例来模拟 Azure 服务总线队列以接收消息

[英]Writing a unit test case in node.js using mocha to mock a azure service bus queue to recive messages

I have written a unit test case,but it is giving error.我写了一个单元测试用例,但它给出了错误。 Please find the code below请找到下面的代码

index.js

const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus");

module.exports = async function (context, myTimer) {

// Define connection string and related Service Bus entity names here
const connectionString = process.env['serviceBusConnectionString'];
const queueName = process.env['serviceBusQueueName'];
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
const queueClient = sbClient.createQueueClient(queueName);
//const receiver = queueClient.createReceiver(ReceiveMode.receiveAndDelete);
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);

const messages = await receiver.receiveMessages(1);
try {

    let payloads = [];
    messages.forEach((msg) => {
        payloads.push(msg.body);
    })

    await queueClient.close();
} catch (err) {
    context.log('Queue message status settle: abandon');
    await messages[0].abandon();
    console.log('Error ', err);
} finally {
    await sbClient.close();
    context.done();
}

}; };

This is the unit test file and I am getting error.Please let me know why I am getting this error enter image description here这是单元测试文件,我收到错误。请告诉我为什么会收到此错误,请在此处输入图片说明

indexTest.js: indexTest.js:

beforeEach(() => {
        const sbClientStub = {
            createQueueClient: sinon.stub().returnsThis(),
            createReceiver: sinon.stub().returnsThis(),
            receiveMessages:sinon.stub(),
            close: sinon.stub(),
        };
        sinon.stub(ServiceBusClient, 'createFromConnectionString').callsFake(() => sbClientStub);
        const ctx = {};
        // const actual = await pushToQueue(message, ctx);
        // sinon.assert.match(actual, 2);
        sinon.assert.calledWithExactly(ServiceBusClient.createFromConnectionString, undefined);
        sinon.assert.calledWithExactly(sbClientStub.createQueueClient, undefined);
        sinon.assert.calledOnce(sbClientStub.createReceiver, undefined );
        //sinon.assert.calledWithExactly(sbClientStub.send.firstCall, { body: 'a' });
        //sinon.assert.calledWithExactly(sbClientStub.send.secondCall, { body: 'b' });
        sinon.assert.calledTwice(sbClientStub.close); 
    });

You should replace every sinon.stub() with sinon.spy() .你应该用sinon.stub()替换每个sinon.spy() The stub will prevent calling the original implementation of methods, but spies will do.存根将阻止调用方法的原始实现,但间谍会这样做。 They basically have the same APIs.它们基本上具有相同的 API。

In order to call the original methods of @azure/service-bus , make sure the resources of @azure/service-bus are ready such as environment variables, service account, queue and so on.为了调用@azure/service-bus的原始方法,请确保@azure/service-bus的环境变量、服务帐户、队列等资源准备就绪。

If you do this, the unit tests are no longer isolated.如果这样做,单元测试就不再是孤立的。 In fact, they are no longer unit tests, but integration tests, or e2e tests.事实上,它们不再是单元测试,而是集成测试,或者 e2e 测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM