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;
);
)
);
javascript express
add a comment |
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;
);
)
);
javascript express
add a comment |
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;
);
)
);
javascript express
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
javascript express
edited Nov 11 at 13:04
haim770
38.9k373108
38.9k373108
asked Nov 11 at 12:32
iagoPassos
61
61
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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