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