存根与sinon链接的诺言

问题描述

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

INSERT INTO BinformatION (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               
----------|---------|----------|---------|---------|-------------------

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...