锡南在打字稿存根 [英] Sinon in typescript stub

查看:112
本文介绍了锡南在打字稿存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打字稿中的Sinon无法正确导入子模块..请在下面的代码
中找到以下代码是文件parent.ts

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

和名为submodule.ts的子模块代码。

and submodule code named as submodule.ts

class submodule{
public submethod(){

    return "hai submodule"
}

}
export default submodule

和以下单元测试脚本

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

})

结果表明预期的false为true。意味着submethod不在这里调用。有人可以帮助我哪里出错了

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

推荐答案

您应该使用链接缝代理要求 ./子模块模块存根。

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

例如,

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                 
---------------|---------|----------|---------|---------|-------------------

这篇关于锡南在打字稿存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆