As per suggestion in question enter link description here I mock readFileSync and mocked my outer function now I want to verify if the variables value is set as expected or not
file.js
const fs1 = require('fs');
let param;
module.export = {
test,
param
}
function test (outputParam) {
param = fs1.readFileSync(outputParam);
}
I have stubbed this readFileSync and it returns specified file content as shown in the test below
When I run the test I want to see the variable param has the file content value
test.spec.js
let expect = require('chai').expect;
let fs = require("fs");
let sinon = require("sinon");
let filejs = require('./file.js');
it('should run only the outer function and test if variable param is set to `this is my file content` ' ,function() {
let someArg ="file.xml";
sinon.stub(fs,'readFileSync').callsFake ((someArg) => {
return "this is my file content";
});
var mock = sinon.mock(filejs);
mock.expects('test').withArgs(someArg);
expect(filejs.param).to.equal('this is my file content');
})
From file.js, As you see property param gets value from "readFileSync" which is stubbed to return a value
When I run the test
expect(filejs.param).to.equal('this is my file content');
AssertionError: expected undefined to equal 'this is my file content'