本文介紹了如何使用sinon和mocha在MySQL查詢節點上模擬Promisify調用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是我使用MySQL的代碼-
import * as mysql from 'mysql';
import {promisify} from 'util';
const connectionParams:any = {
/* set as environment variables */
host: host,
user: user,
password: password,
port: parseInt(port)
};
var connection:any;
const getRecords = async (inputValue: string) => {
//validate inputValue
const userIds: string[] = [];
logger.info("Creating mysql connection");
try {
connection = mysql.createConnection(connectionParams);
const query = promisify(connection.query).bind(connection);
const queryResult = await query({ sql: sqlQuery, timeout: 1000, values: value1, inputValue] });
if (queryResult) {
queryResult.forEach((row) => {
userIds.push(row.userid);
});
}
} catch (error) {
logger.info(error);
// console.log(error);
throw new Error('Could not retrieve user IDs');
} finally {
connection.end();
}
return userIds;
};
這是我的測試-
it('should return a list of records when right inputs are given', async() => {
sinon.stub(process, 'env').value({
'database': 'TESTDB'
});
let dummyArray = [{ userid: 'xyz' }];
let createConnection = {
connect: function(connectionParams: any) {
return Promise.resolve()
},
query : sinon.stub().withArgs({}).callsFake(function (...args): Promise<Object>{
const dummyArray = [{ userid: 'xyz' }];
return new Promise(function(resolve){resolve(dummyArray)});
}),
end: function() {}
};
let mySqlStub = {
createConnection: sinon.stub().returns(createConnection)
};
const dbops = proxyquire('../../lib/dbops', {'mysql': mySqlStub}).default;
expect(await dbops.getUserIds('Delete')).to.deep.equal(['xyz']);
});
如何編寫查詢的FAKE函數?
查詢:sinon.stub().with Args({}).alls sFake(Function(…args):
承諾{
常量虛擬數組=[{userid:‘XYZ’}];
返回新的Promise(function(resolve){resolve(dummyArray)});
})
這對我不起作用。我怎么才能讓它起作用呢?我無法讓存根函數在Main函數中解析和返回預期的值。查詢只是掛起,并在超時后拋出錯誤。存根中的";matchingfakes";方法中出現錯誤。
推薦答案
proxyquire
用于截斷模塊或包的獨立函數導出。由于mysql
是一個對象,您可以通過sinon.stub(obj, 'method')
來存根它的方法。您不需要使用Useproxyquire
包。
即使您使用util.promisify
為NodeJS錯誤優先回調方法(mysql.query(sql, callback)
,回調簽名為function (error, results, ...args): void
)生成Promise版本。您需要使用.callsFake()
為此方法創建模擬實現,并通過調用其回調來觸發Promise版本。
并且,您應該在清除環境變量之后import
該函數。因為當您導入./dbops
模塊時,模塊作用域中的代碼將立即執行,此時,環境變量不會被存根。
例如
dbops.ts
:
import mysql from 'mysql';
import { promisify } from 'util';
const connectionParams: any = {
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
port: parseInt(process.env.PORT || '3306'),
};
var connection: any;
const getRecords = async (inputValue: string) => {
const sqlQuery = 'SELECT * FROM tests';
const value1 = '';
const userIds: string[] = [];
console.info('Creating mysql connection');
try {
connection = mysql.createConnection(connectionParams);
const query = promisify(connection.query).bind(connection);
const queryResult = await query({ sql: sqlQuery, timeout: 1000, values: value1, inputValue });
if (queryResult) {
queryResult.forEach((row) => {
userIds.push(row.userid);
});
}
} catch (error) {
console.info(error);
throw new Error('Could not retrieve user IDs');
} finally {
connection.end();
}
return userIds;
};
export { getRecords };
dbops.test.ts
:
import sinon from 'sinon';
import mysql from 'mysql';
describe('69702002', () => {
it('should return a list of records when right inputs are given', async () => {
sinon.stub(process, 'env').value({
HOST: '127.0.0.1',
USER: 'testuser',
PASSWORD: 'testpwd',
PORT: '3306',
});
const { getRecords } = await import('./dbops');
const dummyArray = [{ userid: 'xyz' }];
let connectionStub = {
query: sinon.stub().callsFake((sql, callback) => {
callback(null, dummyArray);
}),
end: sinon.stub(),
};
sinon.stub(mysql, 'createConnection').returns(connectionStub);
const actual = await getRecords('test input');
sinon.assert.match(actual, ['xyz']);
sinon.assert.calledWithExactly(mysql.createConnection, {
host: '127.0.0.1',
user: 'testuser',
password: 'testpwd',
port: 3306,
});
sinon.assert.calledOnce(connectionStub.end);
});
});
測試結果:
69702002
Creating mysql connection
? should return a list of records when right inputs are given (945ms)
1 passing (952ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 90.48 | 50 | 100 | 90 |
dbops.ts | 90.48 | 50 | 100 | 90 | 27-28
----------|---------|----------|---------|---------|-------------------
這篇關于如何使用sinon和mocha在MySQL查詢節點上模擬Promisify調用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,