Node.js unittest Stubbing Own function












0















Apologies if this was asked before. Here is the module that I'd like to unittest inside file getStuff.js. I'm having difficulty stubbing theresolveThing module used here.



getStuff.js



const resolveThing = require('./resolveThing.js');

module.exports = async function getStuff(target, stuff) {
const { element, test, other } = resolveThing(target);

try {
return element;
} catch (error) {
throw new Error('Did not work.');
}
};


And here is my attempt at the unittest with stubbing using sinon. However when I try to run this it errors out with TypeError: Cannot stub non-existent own property resolveType. Anyone know how I can get this test to work?



const getStuff = require('../com/getStuff');
const resolveThing = require('../com/resolveThing');

const mochaccino = require('mochaccino');

const { expect } = mochaccino;
const sinon = require('sinon');


describe('com.resolveThing', function() {
beforeEach(function () {
sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
});

afterEach(function () {
resolveThing.restore();
});

it('Standard message', function() {
const answer = getAttribute('a','b');
expect(answer).toEqual('a');
});
});









share|improve this question





























    0















    Apologies if this was asked before. Here is the module that I'd like to unittest inside file getStuff.js. I'm having difficulty stubbing theresolveThing module used here.



    getStuff.js



    const resolveThing = require('./resolveThing.js');

    module.exports = async function getStuff(target, stuff) {
    const { element, test, other } = resolveThing(target);

    try {
    return element;
    } catch (error) {
    throw new Error('Did not work.');
    }
    };


    And here is my attempt at the unittest with stubbing using sinon. However when I try to run this it errors out with TypeError: Cannot stub non-existent own property resolveType. Anyone know how I can get this test to work?



    const getStuff = require('../com/getStuff');
    const resolveThing = require('../com/resolveThing');

    const mochaccino = require('mochaccino');

    const { expect } = mochaccino;
    const sinon = require('sinon');


    describe('com.resolveThing', function() {
    beforeEach(function () {
    sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
    });

    afterEach(function () {
    resolveThing.restore();
    });

    it('Standard message', function() {
    const answer = getAttribute('a','b');
    expect(answer).toEqual('a');
    });
    });









    share|improve this question



























      0












      0








      0








      Apologies if this was asked before. Here is the module that I'd like to unittest inside file getStuff.js. I'm having difficulty stubbing theresolveThing module used here.



      getStuff.js



      const resolveThing = require('./resolveThing.js');

      module.exports = async function getStuff(target, stuff) {
      const { element, test, other } = resolveThing(target);

      try {
      return element;
      } catch (error) {
      throw new Error('Did not work.');
      }
      };


      And here is my attempt at the unittest with stubbing using sinon. However when I try to run this it errors out with TypeError: Cannot stub non-existent own property resolveType. Anyone know how I can get this test to work?



      const getStuff = require('../com/getStuff');
      const resolveThing = require('../com/resolveThing');

      const mochaccino = require('mochaccino');

      const { expect } = mochaccino;
      const sinon = require('sinon');


      describe('com.resolveThing', function() {
      beforeEach(function () {
      sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
      });

      afterEach(function () {
      resolveThing.restore();
      });

      it('Standard message', function() {
      const answer = getAttribute('a','b');
      expect(answer).toEqual('a');
      });
      });









      share|improve this question
















      Apologies if this was asked before. Here is the module that I'd like to unittest inside file getStuff.js. I'm having difficulty stubbing theresolveThing module used here.



      getStuff.js



      const resolveThing = require('./resolveThing.js');

      module.exports = async function getStuff(target, stuff) {
      const { element, test, other } = resolveThing(target);

      try {
      return element;
      } catch (error) {
      throw new Error('Did not work.');
      }
      };


      And here is my attempt at the unittest with stubbing using sinon. However when I try to run this it errors out with TypeError: Cannot stub non-existent own property resolveType. Anyone know how I can get this test to work?



      const getStuff = require('../com/getStuff');
      const resolveThing = require('../com/resolveThing');

      const mochaccino = require('mochaccino');

      const { expect } = mochaccino;
      const sinon = require('sinon');


      describe('com.resolveThing', function() {
      beforeEach(function () {
      sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
      });

      afterEach(function () {
      resolveThing.restore();
      });

      it('Standard message', function() {
      const answer = getAttribute('a','b');
      expect(answer).toEqual('a');
      });
      });






      javascript node.js unit-testing tdd sinon






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 15:10







      dredbound

















      asked Nov 15 '18 at 19:49









      dredbounddredbound

      5141411




      5141411
























          1 Answer
          1






          active

          oldest

          votes


















          0














          sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});


          resolveThing must be an object and 'resolveThing' must be a function in the object, an exception is thrown if the property is not already a function.



          I think this is what's hapning in your case.






          share|improve this answer
























          • Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

            – dredbound
            Nov 15 '18 at 21:48











          • maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

            – Lamari Alaa
            Nov 16 '18 at 22:23













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326941%2fnode-js-unittest-stubbing-own-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});


          resolveThing must be an object and 'resolveThing' must be a function in the object, an exception is thrown if the property is not already a function.



          I think this is what's hapning in your case.






          share|improve this answer
























          • Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

            – dredbound
            Nov 15 '18 at 21:48











          • maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

            – Lamari Alaa
            Nov 16 '18 at 22:23


















          0














          sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});


          resolveThing must be an object and 'resolveThing' must be a function in the object, an exception is thrown if the property is not already a function.



          I think this is what's hapning in your case.






          share|improve this answer
























          • Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

            – dredbound
            Nov 15 '18 at 21:48











          • maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

            – Lamari Alaa
            Nov 16 '18 at 22:23
















          0












          0








          0







          sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});


          resolveThing must be an object and 'resolveThing' must be a function in the object, an exception is thrown if the property is not already a function.



          I think this is what's hapning in your case.






          share|improve this answer













          sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});


          resolveThing must be an object and 'resolveThing' must be a function in the object, an exception is thrown if the property is not already a function.



          I think this is what's hapning in your case.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 21:26









          Lamari AlaaLamari Alaa

          1596




          1596













          • Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

            – dredbound
            Nov 15 '18 at 21:48











          • maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

            – Lamari Alaa
            Nov 16 '18 at 22:23





















          • Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

            – dredbound
            Nov 15 '18 at 21:48











          • maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

            – Lamari Alaa
            Nov 16 '18 at 22:23



















          Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

          – dredbound
          Nov 15 '18 at 21:48





          Yes, I suspect that is the case. Do you know how I can modify my test or function for it to work?

          – dredbound
          Nov 15 '18 at 21:48













          maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

          – Lamari Alaa
          Nov 16 '18 at 22:23







          maybe you can create an object where you put your function like this: const resolveThingObject = {resolveThingFn : resolveThing}, and then use your new object: sinon.stub(resolveThingObject,'resolveThingFn').returns({element:'a',test:'b',other:'c'});

          – Lamari Alaa
          Nov 16 '18 at 22:23






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326941%2fnode-js-unittest-stubbing-own-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python