简体   繁体   中英

TypeError: Attempted to wrap undefined property query as function

I'm trying to make a simple basic example of mocking a database driver (to be used in testing later):

'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');

I get the error:

TypeError: Attempted to wrap undefined property query as function

It looks like from the docs that you use mysql.createConnection() to return a connection that then can call 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM