简体   繁体   English

函数调用上的 Javascript 模拟第三方节点库

[英]Javascript mock third-party node library on function call

I'm using sinon and I want to do something very simple, and I cannot find any solution after a lot of searching.我正在使用sinon并且我想做一些非常简单的事情,经过大量搜索后我找不到任何解决方案。

I have class MessageService class looks like this:我有类MessageService类如下所示:

import sb from "@azure/service-bus";

    export class MessageService {

        async handleNewMessage() {

            //some code ....

            await sb.delay(5000);

           //some code ....
        }

    }

I want to test the handleNewMessage method , but inside there is call to third party library sb.delay(5000) (that actually sleep for 5s)我想测试handleNewMessage方法,但是里面有对第三方库sb.delay(5000)调用(实际上睡眠了 5 秒)
I want to replace this function and make it to do nothing .我想替换这个函数,让它什么都不做

In my messageService.spec.js file I tried to import it and replace it, but it's not working.在我的messageService.spec.js文件中,我尝试导入并替换它,但它不起作用。

import sb from "@azure/service-bus";

describe('message service', () => {

 beforeEach(() => {
        sinon.stub(sb, 'delay').returns(()=>{});
    });

 afterEach(() => {
        sinon.restore();
    });

it("handleNewMessage should work", async () => {

        //This is call to the real sb.delay function
        await messageServiceMock.handleNewMessage();

    });

})

Is there any way to replace the original class third party function call?有什么办法可以替代原来的类第三方函数调用吗?

From this comment: https://github.com/sinonjs/sinon/issues/1711#issuecomment-369220753来自此评论: https : //github.com/sinonjs/sinon/issues/1711#issuecomment-369220753

we're not allowed to modify modules that are imported using import.我们不允许修改使用 import 导入的模块。 It looks like imports are read-only views on exports.看起来导入是导出的只读视图。

So, sinon.stub is not able to modify the imported module.因此, sinon.stub 无法修改导入的模块。

Take a look at sb.delay implementation:看看sb.delay实现:

export function delay<T>(t: number, value?: T): Promise<T> {
  return new Promise(resolve => setTimeout(() => resolve(value), t));
}
//...
export { delay }

Here is the solution:这是解决方案:

MessageService.ts : MessageService.ts :

import * as sb from "@azure/service-bus";

export class MessageService {
  async handleNewMessage() {
    await sb.delay(5000);
  }
}

MessageService.test.ts : MessageService.test.ts :

import sinon from "sinon";
import proxyquire from "proxyquire";

describe("message service", () => {
  afterEach(() => {
    sinon.restore();
  });

  it("handleNewMessage should work", async () => {
    const delayStub = sinon.stub().resolves({});
    const { MessageService } = proxyquire("./MessageService.ts", {
      "@azure/service-bus": {
        delay: delayStub,
      },
    });
    const messageServiceMock = new MessageService();
    await messageServiceMock.handleNewMessage();
    sinon.assert.calledWith(delayStub, 5000);
  });
});

Unit test result with 100% coverage: 100% 覆盖率的单元测试结果:

 message service
    ✓ handleNewMessage should work (1343ms)


  1 passing (1s)

------------------------|----------|----------|----------|----------|-------------------|
File                    |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------------|----------|----------|----------|----------|-------------------|
All files               |      100 |      100 |      100 |      100 |                   |
 MessageService.test.ts |      100 |      100 |      100 |      100 |                   |
 MessageService.ts      |      100 |      100 |      100 |      100 |                   |
------------------------|----------|----------|----------|----------|-------------------|

Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59234255源代码: https : //github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59234255

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

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