【发布时间】:2015-03-06 14:20:41
【问题描述】:
我对 Node 比较陌生,并且正在使用 knex 和书架进行项目。我在单元测试我的代码时遇到了一些麻烦,我不确定我做错了什么。
基本上我有一个看起来像这样的模型(称为 VorcuProduct):
var VorcuProduct = bs.Model.extend({
tableName: 'vorcu_products'
});
module.exports.VorcuProduct = VorcuProduct
如果 VorcuProduct 在 DB 上不存在,该函数会保存它。非常简单。执行此操作的函数如下所示:
function subscribeToUpdates(productInformation, callback) {
model.VorcuProduct
.where({product_id: productInformation.product_id, store_id: productInformation.store_id})
.fetch()
.then(function(existing_model) {
if (existing_model == undefined) {
new model.VorcuProduct(productInformation)
.save()
.then(function(new_model) { callback(null, new_model)})
.catch(callback);
} else {
callback(null, existing_model)
}
})
}
在不打数据库的情况下测试这个的正确方法是什么?我是否需要模拟fetch 以返回模型或未定义(取决于测试),然后对save 执行相同操作?我应该为此使用重新布线吗?
如您所见,我有点迷茫,因此我们将不胜感激。
谢谢!
【问题讨论】:
标签: node.js unit-testing bookshelf.js knex.js