相关疑难解决方法(0)

使用Bookshelf.js和knex.js进行单元测试

我是Node的新手,正在使用knex和bookshelf开展项目.我在测试代码时遇到了一些麻烦,我不确定我做错了什么.

基本上我有一个模型(称为VorcuProduct),如下所示:

var VorcuProduct = bs.Model.extend({
    tableName: 'vorcu_products'
});

module.exports.VorcuProduct = VorcuProduct
Run Code Online (Sandbox Code Playgroud)

如果数据库上不存在VorcuProduct,则保存VorcuProduct的函数.非常简单.执行此操作的功能如下所示:

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)
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

在没有命中数据库的情况下测试这个的正确方法是什么?我是否需要模拟fetch返回模型或未定义(取决于测试),然后执行相同的操作save?我应该使用重新连线吗?

如你所见,我有点迷失,所以任何帮助将不胜感激.

谢谢!

unit-testing node.js bookshelf.js knex.js

21
推荐指数
1
解决办法
4323
查看次数

标签 统计

bookshelf.js ×1

knex.js ×1

node.js ×1

unit-testing ×1