【发布时间】:2018-03-09 15:18:59
【问题描述】:
我有以下返回 Bookshelf 模型数据的服务。服务器建立在快递上。我的核心问题是:我想为服务编写测试。其次,我不确定服务测试是否应该包括与数据库的交互;正如您在下面看到的,这些服务目前与 express 交织在一起。
// services.js
import Region from "../../models/region";
import Subregion from "../../models/subregion";
import bookshelf from "../../bookshelf.config";
/** Regions **/
export const getRegions = (req, res, next) => {
Region.forge()
.fetchAll()
.then(regions => {
log.info("Got all regions");
res.status(200).json(regions);
})
.catch(next);
};
/** Subegions **/
export const getSubregions = (req, res, next) => {
Subregion.forge()
.fetchAll({
columns: ["id", "name"],
})
.then(subregions => {
res.status(200).json(subregions);
})
.catch(next);
};
问题
1.测试getRegions之类的函数的正确方法是什么?
2. 最佳实践是否需要从 express 上下文中提取 getRegions 和 getSubregions?
【问题讨论】:
标签: unit-testing express bookshelf.js