【发布时间】:2021-08-18 18:47:10
【问题描述】:
我需要开玩笑地为一个函数编写一个测试。 我的功能如下:
async function fun(conn, input){
const connection = conn.getConnection();
....
// some output gets generated from input
// const processed_input = input???;
....
const x = util.promisify(connection.query).bind(connection);
return /* await */ x(processed_input);
}
我希望将 processed_input 的值传递给函数 x。
我认为像 .toHaveBeenCalledWith 这样的东西应该可以工作我不确定它对于承诺的绑定函数是如何工作的。
我还尝试在fun 调用之前模拟查询,例如conn.getConnection = { query: jest.fn() },但我不确定如何继续使用expect。
更新:
到目前为止,我目前的解决方案是在查询函数中使用 jest expect 语句。
conn.getConnection = {
query: function(processed_input){ //expect processed_input to be; }
}`
希望有更好的方法。
【问题讨论】:
标签: node.js unit-testing jestjs mocking node-promisify