node.js - mssql 连接和查询的笑话
问题描述
无法获得以下模拟测试工作,有人可以帮忙吗?
以下是要测试的源代码:
import { ConnectionPool, Request, VarChar } from "mssql";
export const handler = async (): Promise<any> => {
let connectionPool: ConnectionPool;
let config = {
server: "",
port: 1433,
database: "db",
user: "user",
password: "password",
trustServerCertificate: true
};
try {
connectionPool = new ConnectionPool(config);
connectionPool.connect();
} catch (ConnectionError) {
throw "ConnectionError";
}
try {
let request: Request = connectionPool.request();
request.input('param1', VarChar, 'param1');
request.input('param2', VarChar, 'param2');
let insertQuery = "INSERT INTO TEST_TABLE (column1, column2 ) VALUES (@param1, @param2) ";
return await request.query(insertQuery);
} catch (InsertError) {
throw "InsertError";
}
}
以下是测试:
import {ConnectionPool, Request} from "mssql";
import { handler } from './mock';
const mockQuery = jest.fn().mockReturnValue("result");
const mockInput = jest.fn(() => ({ query: mockQuery }));
const mockRequest = jest.fn(() => ({ input: mockInput }));
jest.mock('mssql', () => ({
ConnectionPool: jest.fn(() => ({
request: mockRequest
}))
}));
describe('test handler', () => {
it('should succeed', async () => {
try {
let res = await handler();
expect(res).toBe("result");
} catch (error) {
expect(error).toBe("");
}
});
});
我需要模拟 mssql 连接和查询并返回一些东西,而不是抛出错误,但我总是抛出错误,如下所示:
expect(received).toBe(expected)
Expected: ""
Received: [TypeError: request.query is not a function]
看不懂这个TypeError,查了@types下的mssql,请求类型确实包含查询函数:public query(command: string): Promise<IResult>;
解决方案
推荐阅读
- c - 我想用 C 语言创建一个折扣计算器,使用一个函数。我想从用户那里获取值。但我的不工作
- scheme - Chicken Scheme.- 如何将复数(例如: (sqrt 2) )转换为整数?不考虑舍入策略
- arrays - 在数组中查找“空帧”(LRU 和 OS 上的模拟)
- spring-boot - 带有 Spring Boot 的 Informix
- python - 无法在列表理解之外检索 itertools 迭代器
- reactjs - 在 React 16 和 Bootstrap 4 中,如何将引导选项卡组件中的每个选项卡映射到 URL?
- python - 在 websocket 的输出中向数据框添加新行
- node.js - 为什么 http.get 在 NodeJS 中需要这么长时间?
- reactjs - 如何在 React 中提取状态的值
- spring - 我想分配对象字段,例如。使用thymeleaf spring boot的greeting.method(可以是post或get)到Form Method属性