微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用sinon测试异步方法

如何解决如何使用sinon测试异步方法

我正在对一个函数进行单元测试,该函数在不等待的情况下调用了另一个异步任务。这样做的原因是因为我不想等待该函数执行。

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();
};

我想对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; 
  });

});

理想情况下,s3Stub应该被调用两次,但是只能被调用一次并且我的测试完成。

我不想在fn2()上使用await,因为我希望fn2()作为独立线程运行。关于如何对此类案例进行单元测试的任何想法?

解决方法

用于测试Table* createTable(int size,int dType,int listLength){ // creating the table Table* table = malloc(sizeof(*table)); table->arr=(Object **)malloc(size * sizeof(Object*)); table->arr[1]->data=1; for(int i=0; i<size;i++){ for(int j=0;j<listLength;j++){ Object* node1 =malloc(sizeof(*node1)); Object* node2; if(table->arr[i]==NULL){ table->arr[i]=node1; node1->next=NULL; } else if(node1!=NULL){ node2=table->arr[i]; for(int z=0;node2->next!=NULL;z++){ node2=node2->next; } node2->next=node1; } } } fn1的单元测试解决方案:

fn2

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

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

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);
    });
  });
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?