Sinon in typescript stub

我们两清 提交于 2020-05-17 08:52:27

问题


Sinon in typescript not able to import sub module propely ..please find below code The below code is file parent.ts

import submodule from './sub-module'
class Parent {

/**
 * name
 */
public parentmethod() {
   let sub = new submodule();
   let result = sub.submethod();
   return result;
}

}

export default Parent

and submodule code named as submodule.ts

class submodule{
public submethod(){

    return "hai submodule"
}

}
export default submodule

and unit test script as below

"use strict";
import chai from 'chai';
import sinon from "sinon";
import submodule from '../src/sub-module'
import  parentmodule from '../src/app'
const expect = chai.expect;
describe("test",function(){
    let stub;
    beforeEach(() => {        
       // stub = sinon.stub(sub ,'saveuser');
     });
    it("test methods",function(){
        stub= sinon.createStubInstance(submodule);
        let parentObj = new parentmodule();
        const user =  parentObj.parentmethod(); 
        expect(stub.submethod.calledOnce).to.be.true;
    })

})

The result is showing that expected false to be true. Means submethod is not calling here .Can any one help me where i went wrong


回答1:


You should use Link Seams and proxyquire to stub ./sub-module module.

E.g.

parent.ts:

import Submodule from './sub-module';

class Parent {
  public parentmethod() {
    let sub = new Submodule();
    let result = sub.submethod();
    return result;
  }
}

export default Parent;

sub-module.ts:

class Submodule {
  public submethod() {
    return 'hai submodule';
  }
}
export default Submodule;

parent.test.ts:

import { expect } from 'chai';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

describe('61798668', () => {
  it('should pass', () => {
    const submoduleStub = {
      submethod: sinon.stub().returns('stubbed data'),
    };
    const SubmoduleStub = sinon.stub().returns(submoduleStub);
    const Parent = proxyquire('./parent', {
      './sub-module': {
        default: SubmoduleStub,
      },
    }).default;
    const parent = new Parent();
    const actual = parent.parentmethod();
    expect(actual).to.be.eq('stubbed data');
    sinon.assert.calledOnce(SubmoduleStub);
    sinon.assert.calledOnce(submoduleStub.submethod);
  });
});

unit test results with coverage report:

  61798668
    ✓ should pass (2797ms)


  1 passing (3s)

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   85.71 |      100 |      50 |   85.71 |                   
 parent.ts     |     100 |      100 |     100 |     100 |                   
 sub-module.ts |      50 |      100 |       0 |      50 | 3                 
---------------|---------|----------|---------|---------|-------------------


来源:https://stackoverflow.com/questions/61798668/sinon-in-typescript-stub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!