简体   繁体   English

如何使用 sinon 测试异步方法

[英]How to test async methods using sinon

I'm unit testing a function which is calling another async task inside it without await.我正在对一个函数进行单元测试,该函数在其中调用另一个异步任务而无需等待。 The reason for that is because I don't want to wait for that function execution.这样做的原因是因为我不想等待该函数执行。

const fn1 = async () => {
  fn2();
  return 'foo';
};

const fn2 = async () => {
  await fn3();
  await fn4();
};

const fn3 = async () => {
  await s3.upload(params).promise();
};

const fn4 = async () => {
  await s3.upload(params).promise();
};

I want to unit test fn1() & fn2() is an async task, & here is what I've tried:我想对 fn1() 和 fn2() 进行单元测试是一个异步任务,这是我尝试过的:

describe("unit test fn1", () => {
  
  let s3Stub;
   
  beforeEach(() => {
     s3Stub = sinon.stub(S3, "upload");
 });

  afterEach(() => {
    s3Stub.restore();
 });
  
  it("fn1 test" , async () => {
     const actualValue = await fn1();
     expect(s3Stub.calledTwice).to.be.true; 
  });

});

Ideally, the s3Stub should have been called twice, but is being called only once & my test gets completed.理想情况下,s3Stub 应该被调用两次,但只被调用一次并且我的测试完成了。

I don't want to use await on fn2(), as I want fn2() to be run as an independent thread.我不想在 fn2() 上使用 await,因为我希望 fn2() 作为独立线程运行。 Any ideas on how do we unit test on such cases?关于我们如何对这种情况进行单元测试的任何想法?

unit test solution for testing fn1 and fn2 :用于测试fn1fn2单元测试解决方案:

index.js : index.js

const s3 = require('./s3');

const fn1 = async () => {
  exports.fn2();
  return 'foo';
};

const fn2 = async () => {
  await exports.fn3();
  await exports.fn4();
};

const fn3 = async () => {
  await s3.upload(params).promise();
};

const fn4 = async () => {
  await s3.upload(params).promise();
};

exports.fn1 = fn1;
exports.fn2 = fn2;
exports.fn3 = fn3;
exports.fn4 = fn4;

s3.js : s3.js

const s3 = {
  upload() {
    return this;
  },
  async promise() {},
};

module.exports = s3;

index.test.js : index.test.js

const fns = require('./');
const sinon = require('sinon');
const { expect } = require('chai');

describe('64705245', () => {
  afterEach(() => {
    sinon.restore();
  });
  describe('fn1', () => {
    it('should return foo', async () => {
      const fn2Stub = sinon.stub(fns, 'fn2').resolves();
      const actual = await fns.fn1();
      expect(actual).to.be.equal('foo');
      sinon.assert.calledOnce(fn2Stub);
    });
  });

  describe('fn2', () => {
    it('should pass', async () => {
      const fn3Stub = sinon.stub(fns, 'fn3').resolves();
      const fn4Stub = sinon.stub(fns, 'fn4').resolves();
      await fns.fn2();
      sinon.assert.calledOnce(fn3Stub);
      sinon.assert.calledOnce(fn4Stub);
    });
  });
});

unit test result:单元测试结果:

  64705245
    fn1
      ✓ should return foo
    fn2
      ✓ should pass


  2 passing (16ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   83.33 |      100 |   33.33 |   83.33 |                   
 index.js |   86.67 |      100 |      50 |   86.67 | 14,18             
 s3.js    |   66.67 |      100 |       0 |   66.67 | 3                 
----------|---------|----------|---------|---------|-------------------

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

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