How to stub a promise such that it always resolves a particular value

若如初见. 提交于 2020-03-06 09:29:44

问题


I want to stub a promise such that it resolves to "failure" always when I run the test. I am not familiar with stubbing a promise.

file.js

const fs1 = require('fs');

let param ="";
module.exports = {
     test,
     test1
}

function test (outputParam) {
  //module.exports.param = fs1.readFileSync(outputParam);
  (async () => {
    module.exports.param = test1();

  }) ();
}

function test1 () {
  let promise = new Promise((resolve,reject) => {
    let promiseVar = "success";
    resolve(promiseVar);
  })

return promise;
}

file.spec.js

let sinon = require("sinon");
let filejs = require('./file.js');
const fs = require('fs');
let expect = require('chai').expect;

it('should stub a promise ' ,function() {

  let someArg ="file.xml";
  sinon.stub(filejs,'test1').usingPromise().resolves("failure");

   filejs.test(someArg);
   expect(filejs.param).to.equal('failure');
 })

but when I ran the test I see this error

AssertionError: expected undefined to equal 'failure'

来源:https://stackoverflow.com/questions/60306699/how-to-stub-a-promise-such-that-it-always-resolves-a-particular-value

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