TypeError:尝试将未定义的属性查询包装为函数

时间:2016-11-11 13:33:38

标签: javascript mysql node.js unit-testing sinon

我试图制作一个模拟数据库驱动程序的简单基本示例(稍后将用于测试):

'use strict';
import mysql from 'mysql';
import sinon from 'sinon';

let mock = sinon.mock(mysql);

mock.expects('query').withExactArgs(`DROP DATABASE IF EXISTS myDatabase`).yields({ "fieldCount": 0, "affectedRows": 1, "insertId": 0, "serverStatus": 258, "warningCount": 0, "message": "", "protocol41": true, "changedRows": 0 });

mysql.query('DROP DATABASE IF EXISTS myDatabase');

我收到错误:

TypeError: Attempted to wrap undefined property query as function

1 个答案:

答案 0 :(得分:0)

从文档看起来,您使用mysql.createConnection()返回一个可以调用connection.query的连接。

'use strict';
import mysql from 'mysql';
import sinon from 'sinon';

let mock = sinon.mock(mysql.createConnection());

mock.expects('query').withExactArgs(`DROP DATABASE IF EXISTS myDatabase`).yields({ "fieldCount": 0, "affectedRows": 1, "insertId": 0, "serverStatus": 258, "warningCount": 0, "message": "", "protocol41": true, "changedRows": 0 });

mysql.query('DROP DATABASE IF EXISTS myDatabase');