How to check the property value after mocking a function : Assertion Error ,mocha

前提是你 提交于 2020-03-05 02:11:28

问题


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'


回答1:


Note the correct spelling of module.exports - not module.export.

In your file.js the variable param is not initialized, so it will get the value undefined and will be exported with that value, too. The exported value does not change if you modify param later. An export only binds a property to a value, not to a variable. And in fact, you don't even need a local variable. To change the value of an export dynamically, at least in Node, you simply reassign the property on module.exports.

const fs1 = require('fs');

module.exports = {
     test
}

function test (outputParam) {
    module.exports.param = fs1.readFileSync(outputParam);
}

As for your test.spec.js, you were already very close to get it to work. After stubbing the method(s) that should not be called under the hub, just call your test function, passing real parameters. No mock is required there.

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

  filejs.test(someArg);

  expect(filejs.param).to.equal('this is my file content');

});



来源:https://stackoverflow.com/questions/60299439/how-to-check-the-property-value-after-mocking-a-function-assertion-error-moch

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