存根与sinon链接的诺言

问题描述

INSERT INTO BESTABLISHMENT (BPERSONID,ESTID) VALUES ('test','estid1');

INSERT INTO B@R_983_4045@ION (ESTID,TAXATION,CRN) VALUES ( 'estid1','test','test2');

let image = await object .firstCall(params) .promise() .then(data => { return data }) .catch(err => { console.(err) }); 对链式promise进行存根的方式是什么?我尝试了以下方法,但是没有运气

sinon

解决方法

单元测试解决方案:

index.ts

import { object } from './obj';

export async function main() {
  const params = {};
  return object
    .firstCall(params)
    .promise()
    .then((data) => {
      return data;
    })
    .catch((err) => {
      console.log(err);
    });
}

obj.ts

export const object = {
  firstCall(params) {
    return this;
  },async promise() {
    return 'real data';
  },};

index.test.ts

import { main } from './';
import { object } from './obj';
import sinon from 'sinon';
import { expect } from 'chai';

describe('64795845',() => {
  afterEach(() => {
    sinon.restore();
  });
  it('should return data',async () => {
    const promiseStub = sinon.stub(object,'promise').resolves('fake data');
    const firtCallStub = sinon.stub(object,'firstCall').returnsThis();
    const actual = await main();
    expect(actual).to.be.equal('fake data');
    sinon.assert.calledWithExactly(firtCallStub,{});
    sinon.assert.calledOnce(promiseStub);
  });
});

单元测试结果:

  64795845
    ✓ should return data


  1 passing (32ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   66.67 |      100 |      40 |   66.67 |                   
 index.ts |   83.33 |      100 |   66.67 |   83.33 | 12                
 obj.ts   |   33.33 |      100 |       0 |   33.33 | 3-6               
----------|---------|----------|---------|---------|-------------------