简体   繁体   English

sinon存根模块功能

[英]sinon stub on module function

i'm trying test a es6 class, but i don't know how stub a function module whit sinon. 我正在尝试测试es6类,但我不知道如何将功能模块添加到sinon。 The test not coverage line under sm.callSoap function sm.callSoap函数下的测试未覆盖行

I try this: 我尝试这样:

module.js module.js

 function soapModule(){
      this.callSoap = (id) => {
         ....//some code
          return new Promise((resolve,reject) =>{
             return resolve("whatever");
          }
      }
 }

index.js (this is the index of the module) index.js (这是模块的索引)

 "use strict";

 var soapModule = require('./module/module');

 module.exports.soapModule = soapModule;

my-class.js 我-class.js

import {soapModule} from "soap-client"

export default class MyClass {

   constructor(){
     console.log("instance created");
   }

   myMethod(id){
     let sm = new soapModule();

     return sm.callSoap(id)
            .then(result => {
                console.log(result);
            }).catch(e => {
                console.log("Error :" + e);
            })
   }
}

test.js test.js

import MyClass from "../src/my-class";
import {soapModule} from "soap-client";
import sinon from 'sinon';

describe("Test MyClass",()=>{

    let myclass;
    let soap;
    let stub;

    before(()=>{
        myclass = new MyClass();
        soap = new soapModule();
        stub = sinon.stub(soap,'callSoap');
    });

    it("test a", ()=>{
        let fakeout = {
            resp : "tada!"
        }
        stub.resolves(fakeout);
        myclass.myMethod(1);
    });
});

i try to stub on soapModule but generate this error: 我尝试对soapModule进行存根,但生成此错误:

Cannot stub non-existent own property callSoap 无法存根不存在的自己的财产callSoap

finally, i had to change the module to ECMAScript 6 syntax. 最后,我不得不将模块更改为ECMAScript 6语法。

so, my new module looks like this: 因此,我的新模块如下所示:

module.js module.js

  export function callSoap(id){
     ....//some code
      return new Promise((resolve,reject) =>{
         return resolve("whatever");
      }
  }

when I change to ECMAScript 6 syntax, i implement babel-cli to compile to EC5, so the index change from: 当我更改为ECMAScript 6语法时,我实现了babel-cli来编译为EC5,因此索引从:

   var soapModule = require('./module/module'); 

to

    var soapModule = require('./lib/module'); //<-- this is the build output folder

then, the unit-test looks like this: 然后,单元测试如下所示:

import MyClass from "../src/my-class";
import {soapModule} from "soap-client";
import sinon from 'sinon';

describe("Test MyClass",()=>{

let myclass;
let stub;

before(()=>{
    myclass = new MyClass();
    stub = sinon.stub(soap,'callSoap');
});

it("test a", ()=>{
    let fakeout = {
        resp : "tada!"
    }
    stub.resolves(fakeout);
    myclass.myMethod(1).then(result =>{
          console.log(result) //<----- this is the fakeout
       }
    )
  });
});

I also noticed that you had stubbed the callSoap method of an instance of soapModule. 我还注意到,您已经对soapModule实例的callSoap方法进行了处理。 It needs to be the stubbed on the prototype of soapModule so when you create an instance inside of myMethod it has the stubbed version. 它必须是soapModule原型上的存根,因此当您在myMethod内创建实例时,它具有存根的版本。

import MyClass from "../src/my-class";
import {soapModule} from "soap-client";
import sinon from 'sinon';

describe("Test MyClass",()=>{

    let myclass;
    let stub;
    let stubP;

    before(()=>{
        myclass = new MyClass();
        stub = sinon.stub(soapModule.prototype, 'callSoap');
        stubP = sinon.stub(); // second stub to be used as a promise
        stub.returns(stubP);
    });

    after(() => {
      stub.restore();
    });

    it("test a", ()=>{
        let fakeout = {
            resp : "tada!"
        }
        myclass.myMethod(1);
        stubP.resolves(fakeout);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM