[英]How to test node.js module which returns anonymous function?
I'm writing small middleware for express.js route, but when I came to unit test this code I stuck, and I don't know how to properly test it using mocha, sinon and chai. 我正在为express.js路由编写小型中间件,但是当我来测试这个代码时,我卡住了,我不知道如何使用mocha,sinon和chai正确测试它。
My middleware code has entrypoint something like this: 我的中间件代码有这样的入口点:
const searchByQuerystring = require('./search-by-querystring');
const searchByMarker = require('./search-by-marker');
module.exports = (req, res, next) => {
if (req.marker) {
searchByMarker(req, res, next);
} else if (req.query) {
searchByQuerystring(req, res, next);
} else {
next();
}
};
, and during unit test I would like to test if method searchByMarker or searchByQuerystring was called. ,在单元测试期间,我想测试方法searchByMarker或searchByQuerystring是否被调用。
So I start with writing this test, 所以我开始写这个测试,
it('Should call search by querystring if querystring is present', () => {
const req = httpMocks.createRequest({
query: {
value: 1000
}
}),
res = httpMocks.createResponse(),
next = sandbox.spy();
searchIndex(req, res, next);
sinon.assert.calledOnce(next);
});
where my middleware should use searchByQuerystring for process request and I would like to test it if searchByQuerystring method was called, but I really don't know how to do that, maybe i just should write this code in some other way, I really don't want to use libraries like proxyquire . 我的中间件应该使用searchByQuerystring来处理请求,如果调用了searchByQuerystring方法我想测试它,但我真的不知道怎么做,也许我应该用其他方式编写这个代码,我真的不喜欢我想使用像proxyquire这样的库 。
Maybe my module is doing too much work (according to Single Responsibility Principle) - but whole middleware is for build search object where on start I just need to find out from which place parameters will came, so I thought that it is a good idea to put this logic on start of the middleware - and I just should have two middlewares? 也许我的模块做了太多的工作(根据单一责任原则) - 但是整个中间件是用于构建搜索对象,在开始时我只需要找出参数将来自哪个地方,所以我认为这是一个好主意把这个逻辑放在中间件的启动上 - 我应该有两个中间件?
Please for any help, suggestions about this. 请提供任何帮助,建议。
Alright, 好的,
So putting this post help me to to find a solution. 所以把这篇文章帮我找到解决方案。 I rewrite my middleware entry point, to looking like this 我重写了我的中间件入口点,看起来像这样
const searchRequestParser = require('./search-request-parser');
const search = require('../../../lib/search');
module.exports = (req, res, next) => {
const searchRequest = searchRequestParser(req);
if (searchRequest) {
const searchCriteria = search(searchRequest.resourceToSearch);
req.searchQuery =
searchCriteria.buildQuery(searchRequest.queryParameters);
}
next();
};
And test 并测试
it('Should call search by querystring if querystring is present', () => {
const req = httpMocks.createRequest({
method: 'GET',
params: {
resourceToSearch: 'debts'
},
query: {
value: 1000
}
}),
res = httpMocks.createResponse(),
next = sandbox.spy();
searchIndex(req, res, next);
expect(req).to.have.a.property('searchQuery');
});
So methods searchByMarker or searchByQuerystring are gone and replaced by method searchRequestParser with output, then I process this output in code which as it turned out was duplicated previously in methods searchByMarker or searchByQuerystring . 所以方法searchByMarker或searchByQuerystring已经消失并被带有输出的方法searchRequestParser取代,然后我在代码中处理这个输出,因为它原来是在方法searchByMarker或searchByQuerystring中重复的 。
Thanks to return outputs from functions I used, I didn't have to focus on mocking/stubing those functions. 感谢我使用的函数的返回输出,我不必专注于模拟/存根这些函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.