javascript – 如何针对Knex进行单元测试时,如何模拟假数据库?

栏目: 编程工具 · 发布时间: 7年前

内容简介:我一直在使用翻译自:https://stackoverflow.com/questions/28165063/how-can-i-mock-a-fake-database-for-when-unit-testing-against-knex
我已成功使用 Knex

连接到后端数据库.但我希望能够对我的代码进行单元测试.有没有办法模拟数据库连接?

我尝试过使用 proxyquire ,但似乎无法让它工作.

问题似乎与Knex的初始化方式有关.

var knex = require('knex')({
  client: 'mysql',
  connection: {}
});

我设置knex在我的单元测试中被嘲笑.

myService = proxyquire('../app/myService', {
        'knex': knexProxy
});

我的服务包括knex.

var knex = require('knex').knex,

当我的服务运行查询时,它会失败.

var sql = knex("table_name");
sql.insert(rowToInsert, "auto_increment_id");
sql.then(function (insertId) {
    resolve();
}, function (err) {
    reject(err);
});

出于某种原因,我似乎无法在尝试连接之前捕获请求.

我也试图创建一个 custom Knex Client ,但这还没有奏效.

我一直在使用 in-memory Sqlite3 databases 进行自动化测试,取得了巨大的成功.这不是真正的单元测试,但它的运行速度比 MySQL 或PostgreSQL快得多.我已发布有关此解决方案 on a different question 的更多详细信息.

翻译自:https://stackoverflow.com/questions/28165063/how-can-i-mock-a-fake-database-for-when-unit-testing-against-knex


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Distributed Algorithms

Distributed Algorithms

Wan Fokkink / The MIT Press / 2013-12-6 / USD 40.00

This book offers students and researchers a guide to distributed algorithms that emphasizes examples and exercises rather than the intricacies of mathematical models. It avoids mathematical argumentat......一起来看看 《Distributed Algorithms》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具