Sinon Not Mocking db-mysql Node.js library

Sinon Not Mocking db-mysql Node.js library

本文关键字:js library Node db-mysql Not Mocking Sinon      更新时间:2023-09-26

我正在尝试在 Node 中使用 Sinon 和 Mocha 来模拟我的数据库.js进行测试。我尝试了以下方法:

var sinon = require('sinon');
var mysql = require('db-mysql');
beforeEach(function() {
  var db = sinon.mock(mysql);
  db.expects('execute');
});

但我不断收到以下错误:TypeError: Attempted to wrap undefined property execute as function

我假设这是在模拟类而不是数据库实例。所以我通过做var db = sinon.mock(new mysql.Database());来嘲笑这个实例。当我这样做时,无论参数是什么,db-mysql实例的所有有效方法都会传递,例如 db.connect()db.query() 。我无法设置行为。要设置行为,我正在尝试在db上调用.expects,但出现以下错误:

TypeError: Object [object Object] has no method 'expects'

设置预期行为的正确方法是什么?此外,如何测试同一功能的多种行为?我是否需要根据测试预期在每个测试中执行此操作?

这是因为

execute是对象的一部分query而不是数据库本身。

https://github.com/mariano/node-db-mysql#quick-start