gpt4 book ai didi

node.js - node.js 快速路由/ Controller 中基于 promise 的单元测试代码

转载 作者:搜寻专家 更新时间:2023-11-01 00:32:01 24 4
gpt4 key购买 nike

最近我在我的 rest api express 应用程序中从使用回调切换到使用 promise。但是我在使用 promise 的异步行为对路由/ Controller 进行单元测试时遇到了麻烦。这是需要进行单元测试的示例代码。

var handler =  function (req, res, next) {
var query = {},
var options = {
sort: { updatedAt: -1 },
limit: 10
};
if (req.query.before) {
query.updatedAt = { $lt: req.query.before };
}
// User.findAsync returns bluebird promise
User.findAsync(query, null, options).then(function (user) {
res.json(user);
}).catch(function (e) {
next(e);
});
}
router.get('/api/users', handler);

我测试上述代码的方法是监视 req、next 和 User.findAsync 并检查它们是否使用正确的参数调用。但是由于 promise 的异步行为,我无法检查是否调用了 res.json 或 next。

我尝试对 findAsync 进行 stub 以返回已解决的 promise (Promise.resolve(user))。但仍然异步执行回调。

我不确定我是否在测试 express 应用程序的正确轨道上。

在良好分离中测试这种代码的好策略是什么?

我也听说过使用 supertest。但对我来说,使用 supertest 从 http 端点进行测试感觉更像是集成测试,它不是单元测试,而且非常昂贵。

此外,一般来说,我想知道尝试用单元测试(模型、 Controller 、中间件等)覆盖所有代码是否是一种好的做法,以及这样做的好的策略或技术是什么。或者如果它足以用 super 测试测试 http 端点。

最佳答案

如果您被测试的方法没有返回 promise ,那么您就不能在 Mocha 中使用 promise 语法。您可以像测试任何其他异步方法一样测试您的方法 - 使用 done 作为 it 的参数。假设我们要测试您的 handler 函数:

var handler =  function (req, res, next) {
//...
User.findAsync(query, null, options).then(function (user) {
res.json(user);
}).catch(function (e) {
next(e);
});
}

我们可以这样写一个测试:

describe("The handler", function(){
it("calls res.json", function(done){ // note the done argument
handler({query: {before: 5}, // mock request
{json: done} // res.json calls our `done` param of this function
function(){ throw new Error("error called"); });
});
});

请注意,我们模拟了请求、响应和 next 处理程序。我们的模拟响应有一个 json 方法,让测试知道它已完成(如果您想在其中进行断言,这可以是一个函数)并且如果 next 被调用而不是我们抛出信号它不是什么那应该发生。

关于node.js - node.js 快速路由/ Controller 中基于 promise 的单元测试代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com