【发布时间】:2021-04-23 20:07:28
【问题描述】:
我想模拟 redis-session 函数。
static async createSession(userData: string, userId: Uuid, ipAddress: string = 'NA'): Promise<string> {
try {
const rs = this.getRedisConnection();
return new Promise<string>((resolve, reject): void => {
rs.create(
{
app: Configs.rsSessionApp,
id: userId,
ip: ipAddress,
ttl: Configs.rsDefaultTtl,
d: {
data: userData,
},
},
(err: object, resp: { token: string }): void => {
if (resp !== undefined && resp.token.length > 0) {
return resolve(resp.token);
}
if (err != null) {
reject(err);
}
},
);
});
} catch (error) {
return Promise.reject(error.message);
}
}
我想让这一行执行 (err: object, resp: { token: string }): void => {} 如何使用开玩笑的单元测试来实现或解决这个问题? 另一个抛出错误的单元测试。
【问题讨论】:
-
this.getRedisConnection()的返回值是多少? -
它返回一个具有给定端口号和 url 的新 redis 会话。
return new redisSession({ host: Configs.redisConnectionInfo.host, port: Configs.redisConnectionInfo.port, namespace: Configs.rsapp});
标签: node.js typescript redis jestjs mocking