Trying to test if next function was called on router









up vote
1
down vote

favorite












i don't know if the questions is very clear, but probably looking at the code you'll understand.



i'm trying to see if the next() function was called on my router.



But everytime i use the debugger, i watch that the next i passed as a stub to my router don't get on my router as a stub, looks like it get lost on the way. If i extract the callback from the router and exports it separately, it works just fine. But i didn't want to separate things and export them.






const express = require('express');
const mostReadRouter = express.Router();
const mostReadBackEnd = require('../services/most-read-back-end');
const translateMostReadList = require('../services/most-read-service');

mostReadRouter.get('/', (req, res, next) =>

let mostReadUrl = req.query.most_read_url;

if (!mostReadUrl)
logger.error('param most_read_url is required');
res.status(400).send('param most_read_url is required');
return;


let sendSucces = mostRead =>
logger.info(`sending most read list for url: $mostReadUrl`);
res.json(mostRead);
;

let sendError = error =>
if (isNotFoundError(error))
next();
else
next(error);

;

mostReadBackEnd
.getMostReadList(mostReadUrl)
.then(translateMostReadList, sendError)
.then(sendSucces, sendError)
.catch(sendError);
);

module.exports = mostReadRouter;








const chai = require('chai');
const expect = chai;
chai.use(require('sinon-chai'));
const sinon = require('sinon');
const sandbox = sinon.createSandbox();
const proxyQuire = require('proxyquire');
const statusStub = sandbox.stub();
const sendStub = sandbox.stub();
const getMostReadListStub = sandbox.stub();
const translateStub = sandbox.stub();
const jsonStub = sandbox.stub();
const thenStub = sandbox.stub();
process.env.CONFIGURATOR_API = 'xpto';


const router = proxyQuire('../../app/routes/most-read-router',
'../services/most-read-back-end':
getMostReadList: getMostReadListStub
,
'../services/most-read-service':
translate: translateStub

);

describe('MostReadRouter', () =>
afterEach(() => sandbox.reset());

describe('#get(request,response,next)', () =>
it.only('should call next() when getMostReadList does not work` ', async () =>

getMostReadListStub.rejects(new Error('the error'));

let req =
method: 'GET',
url: '/',
query:
most_read_url: 'http://beatiful_url.com'

;

let res =
json: jsonStub
;

let next = sandbox.stub();


await router(req, res, next);

expect(next).to.be.calledOnce;
);
)
);












share|improve this question



























    up vote
    1
    down vote

    favorite












    i don't know if the questions is very clear, but probably looking at the code you'll understand.



    i'm trying to see if the next() function was called on my router.



    But everytime i use the debugger, i watch that the next i passed as a stub to my router don't get on my router as a stub, looks like it get lost on the way. If i extract the callback from the router and exports it separately, it works just fine. But i didn't want to separate things and export them.






    const express = require('express');
    const mostReadRouter = express.Router();
    const mostReadBackEnd = require('../services/most-read-back-end');
    const translateMostReadList = require('../services/most-read-service');

    mostReadRouter.get('/', (req, res, next) =>

    let mostReadUrl = req.query.most_read_url;

    if (!mostReadUrl)
    logger.error('param most_read_url is required');
    res.status(400).send('param most_read_url is required');
    return;


    let sendSucces = mostRead =>
    logger.info(`sending most read list for url: $mostReadUrl`);
    res.json(mostRead);
    ;

    let sendError = error =>
    if (isNotFoundError(error))
    next();
    else
    next(error);

    ;

    mostReadBackEnd
    .getMostReadList(mostReadUrl)
    .then(translateMostReadList, sendError)
    .then(sendSucces, sendError)
    .catch(sendError);
    );

    module.exports = mostReadRouter;








    const chai = require('chai');
    const expect = chai;
    chai.use(require('sinon-chai'));
    const sinon = require('sinon');
    const sandbox = sinon.createSandbox();
    const proxyQuire = require('proxyquire');
    const statusStub = sandbox.stub();
    const sendStub = sandbox.stub();
    const getMostReadListStub = sandbox.stub();
    const translateStub = sandbox.stub();
    const jsonStub = sandbox.stub();
    const thenStub = sandbox.stub();
    process.env.CONFIGURATOR_API = 'xpto';


    const router = proxyQuire('../../app/routes/most-read-router',
    '../services/most-read-back-end':
    getMostReadList: getMostReadListStub
    ,
    '../services/most-read-service':
    translate: translateStub

    );

    describe('MostReadRouter', () =>
    afterEach(() => sandbox.reset());

    describe('#get(request,response,next)', () =>
    it.only('should call next() when getMostReadList does not work` ', async () =>

    getMostReadListStub.rejects(new Error('the error'));

    let req =
    method: 'GET',
    url: '/',
    query:
    most_read_url: 'http://beatiful_url.com'

    ;

    let res =
    json: jsonStub
    ;

    let next = sandbox.stub();


    await router(req, res, next);

    expect(next).to.be.calledOnce;
    );
    )
    );












    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      i don't know if the questions is very clear, but probably looking at the code you'll understand.



      i'm trying to see if the next() function was called on my router.



      But everytime i use the debugger, i watch that the next i passed as a stub to my router don't get on my router as a stub, looks like it get lost on the way. If i extract the callback from the router and exports it separately, it works just fine. But i didn't want to separate things and export them.






      const express = require('express');
      const mostReadRouter = express.Router();
      const mostReadBackEnd = require('../services/most-read-back-end');
      const translateMostReadList = require('../services/most-read-service');

      mostReadRouter.get('/', (req, res, next) =>

      let mostReadUrl = req.query.most_read_url;

      if (!mostReadUrl)
      logger.error('param most_read_url is required');
      res.status(400).send('param most_read_url is required');
      return;


      let sendSucces = mostRead =>
      logger.info(`sending most read list for url: $mostReadUrl`);
      res.json(mostRead);
      ;

      let sendError = error =>
      if (isNotFoundError(error))
      next();
      else
      next(error);

      ;

      mostReadBackEnd
      .getMostReadList(mostReadUrl)
      .then(translateMostReadList, sendError)
      .then(sendSucces, sendError)
      .catch(sendError);
      );

      module.exports = mostReadRouter;








      const chai = require('chai');
      const expect = chai;
      chai.use(require('sinon-chai'));
      const sinon = require('sinon');
      const sandbox = sinon.createSandbox();
      const proxyQuire = require('proxyquire');
      const statusStub = sandbox.stub();
      const sendStub = sandbox.stub();
      const getMostReadListStub = sandbox.stub();
      const translateStub = sandbox.stub();
      const jsonStub = sandbox.stub();
      const thenStub = sandbox.stub();
      process.env.CONFIGURATOR_API = 'xpto';


      const router = proxyQuire('../../app/routes/most-read-router',
      '../services/most-read-back-end':
      getMostReadList: getMostReadListStub
      ,
      '../services/most-read-service':
      translate: translateStub

      );

      describe('MostReadRouter', () =>
      afterEach(() => sandbox.reset());

      describe('#get(request,response,next)', () =>
      it.only('should call next() when getMostReadList does not work` ', async () =>

      getMostReadListStub.rejects(new Error('the error'));

      let req =
      method: 'GET',
      url: '/',
      query:
      most_read_url: 'http://beatiful_url.com'

      ;

      let res =
      json: jsonStub
      ;

      let next = sandbox.stub();


      await router(req, res, next);

      expect(next).to.be.calledOnce;
      );
      )
      );












      share|improve this question















      i don't know if the questions is very clear, but probably looking at the code you'll understand.



      i'm trying to see if the next() function was called on my router.



      But everytime i use the debugger, i watch that the next i passed as a stub to my router don't get on my router as a stub, looks like it get lost on the way. If i extract the callback from the router and exports it separately, it works just fine. But i didn't want to separate things and export them.






      const express = require('express');
      const mostReadRouter = express.Router();
      const mostReadBackEnd = require('../services/most-read-back-end');
      const translateMostReadList = require('../services/most-read-service');

      mostReadRouter.get('/', (req, res, next) =>

      let mostReadUrl = req.query.most_read_url;

      if (!mostReadUrl)
      logger.error('param most_read_url is required');
      res.status(400).send('param most_read_url is required');
      return;


      let sendSucces = mostRead =>
      logger.info(`sending most read list for url: $mostReadUrl`);
      res.json(mostRead);
      ;

      let sendError = error =>
      if (isNotFoundError(error))
      next();
      else
      next(error);

      ;

      mostReadBackEnd
      .getMostReadList(mostReadUrl)
      .then(translateMostReadList, sendError)
      .then(sendSucces, sendError)
      .catch(sendError);
      );

      module.exports = mostReadRouter;








      const chai = require('chai');
      const expect = chai;
      chai.use(require('sinon-chai'));
      const sinon = require('sinon');
      const sandbox = sinon.createSandbox();
      const proxyQuire = require('proxyquire');
      const statusStub = sandbox.stub();
      const sendStub = sandbox.stub();
      const getMostReadListStub = sandbox.stub();
      const translateStub = sandbox.stub();
      const jsonStub = sandbox.stub();
      const thenStub = sandbox.stub();
      process.env.CONFIGURATOR_API = 'xpto';


      const router = proxyQuire('../../app/routes/most-read-router',
      '../services/most-read-back-end':
      getMostReadList: getMostReadListStub
      ,
      '../services/most-read-service':
      translate: translateStub

      );

      describe('MostReadRouter', () =>
      afterEach(() => sandbox.reset());

      describe('#get(request,response,next)', () =>
      it.only('should call next() when getMostReadList does not work` ', async () =>

      getMostReadListStub.rejects(new Error('the error'));

      let req =
      method: 'GET',
      url: '/',
      query:
      most_read_url: 'http://beatiful_url.com'

      ;

      let res =
      json: jsonStub
      ;

      let next = sandbox.stub();


      await router(req, res, next);

      expect(next).to.be.calledOnce;
      );
      )
      );








      const express = require('express');
      const mostReadRouter = express.Router();
      const mostReadBackEnd = require('../services/most-read-back-end');
      const translateMostReadList = require('../services/most-read-service');

      mostReadRouter.get('/', (req, res, next) =>

      let mostReadUrl = req.query.most_read_url;

      if (!mostReadUrl)
      logger.error('param most_read_url is required');
      res.status(400).send('param most_read_url is required');
      return;


      let sendSucces = mostRead =>
      logger.info(`sending most read list for url: $mostReadUrl`);
      res.json(mostRead);
      ;

      let sendError = error =>
      if (isNotFoundError(error))
      next();
      else
      next(error);

      ;

      mostReadBackEnd
      .getMostReadList(mostReadUrl)
      .then(translateMostReadList, sendError)
      .then(sendSucces, sendError)
      .catch(sendError);
      );

      module.exports = mostReadRouter;





      const express = require('express');
      const mostReadRouter = express.Router();
      const mostReadBackEnd = require('../services/most-read-back-end');
      const translateMostReadList = require('../services/most-read-service');

      mostReadRouter.get('/', (req, res, next) =>

      let mostReadUrl = req.query.most_read_url;

      if (!mostReadUrl)
      logger.error('param most_read_url is required');
      res.status(400).send('param most_read_url is required');
      return;


      let sendSucces = mostRead =>
      logger.info(`sending most read list for url: $mostReadUrl`);
      res.json(mostRead);
      ;

      let sendError = error =>
      if (isNotFoundError(error))
      next();
      else
      next(error);

      ;

      mostReadBackEnd
      .getMostReadList(mostReadUrl)
      .then(translateMostReadList, sendError)
      .then(sendSucces, sendError)
      .catch(sendError);
      );

      module.exports = mostReadRouter;





      const chai = require('chai');
      const expect = chai;
      chai.use(require('sinon-chai'));
      const sinon = require('sinon');
      const sandbox = sinon.createSandbox();
      const proxyQuire = require('proxyquire');
      const statusStub = sandbox.stub();
      const sendStub = sandbox.stub();
      const getMostReadListStub = sandbox.stub();
      const translateStub = sandbox.stub();
      const jsonStub = sandbox.stub();
      const thenStub = sandbox.stub();
      process.env.CONFIGURATOR_API = 'xpto';


      const router = proxyQuire('../../app/routes/most-read-router',
      '../services/most-read-back-end':
      getMostReadList: getMostReadListStub
      ,
      '../services/most-read-service':
      translate: translateStub

      );

      describe('MostReadRouter', () =>
      afterEach(() => sandbox.reset());

      describe('#get(request,response,next)', () =>
      it.only('should call next() when getMostReadList does not work` ', async () =>

      getMostReadListStub.rejects(new Error('the error'));

      let req =
      method: 'GET',
      url: '/',
      query:
      most_read_url: 'http://beatiful_url.com'

      ;

      let res =
      json: jsonStub
      ;

      let next = sandbox.stub();


      await router(req, res, next);

      expect(next).to.be.calledOnce;
      );
      )
      );





      const chai = require('chai');
      const expect = chai;
      chai.use(require('sinon-chai'));
      const sinon = require('sinon');
      const sandbox = sinon.createSandbox();
      const proxyQuire = require('proxyquire');
      const statusStub = sandbox.stub();
      const sendStub = sandbox.stub();
      const getMostReadListStub = sandbox.stub();
      const translateStub = sandbox.stub();
      const jsonStub = sandbox.stub();
      const thenStub = sandbox.stub();
      process.env.CONFIGURATOR_API = 'xpto';


      const router = proxyQuire('../../app/routes/most-read-router',
      '../services/most-read-back-end':
      getMostReadList: getMostReadListStub
      ,
      '../services/most-read-service':
      translate: translateStub

      );

      describe('MostReadRouter', () =>
      afterEach(() => sandbox.reset());

      describe('#get(request,response,next)', () =>
      it.only('should call next() when getMostReadList does not work` ', async () =>

      getMostReadListStub.rejects(new Error('the error'));

      let req =
      method: 'GET',
      url: '/',
      query:
      most_read_url: 'http://beatiful_url.com'

      ;

      let res =
      json: jsonStub
      ;

      let next = sandbox.stub();


      await router(req, res, next);

      expect(next).to.be.calledOnce;
      );
      )
      );






      javascript express






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 13:04









      haim770

      38.9k373108




      38.9k373108










      asked Nov 11 at 12:32









      iagoPassos

      61




      61



























          active

          oldest

          votes











          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',
          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%2f53248797%2ftrying-to-test-if-next-function-was-called-on-router%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53248797%2ftrying-to-test-if-next-function-was-called-on-router%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin