【问题标题】:Jest mock for redis-sessions functions用于 redis 会话功能的玩笑模拟
【发布时间】: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


【解决方案1】:

try...catch...无法捕捉到异步代码的异常,可以去掉。

您可以使用jest.spyOn(object, methodName) 模拟RedisSession.getRedisConnection() 方法并返回一个模拟的redis 连接。然后,模拟rs.create()方法的实现,就可以在你的测试用例中获取匿名回调函数了。

最后,使用模拟参数手动调用匿名回调函数。

例如

index.ts:

import RedisSessions from 'redis-sessions';

interface Uuid {}

export class RedisSession {
  static async createSession(userData: string, userId: Uuid, ipAddress: string = 'NA'): Promise<string> {
    const rs = this.getRedisConnection();
    return new Promise<string>((resolve, reject): void => {
      rs.create(
        {
          app: 'test',
          id: userId,
          ip: ipAddress,
          ttl: 3600,
          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);
          }
        }
      );
    });
  }

  static getRedisConnection() {
    return new RedisSessions({ host: '127.0.0.1', port: '6379', namespace: 'rs' });
  }
}

index.test.ts:

import { RedisSession } from './';

describe('67220364', () => {
  it('should create redis connection', async () => {
    const mRs = {
      create: jest.fn().mockImplementationOnce((option, callback) => {
        callback(null, { token: '123' });
      }),
    };
    const getRedisConnectionSpy = jest.spyOn(RedisSession, 'getRedisConnection').mockReturnValueOnce(mRs);
    const actual = await RedisSession.createSession('teresa teng', '1');
    expect(actual).toEqual('123');
    expect(getRedisConnectionSpy).toBeCalledTimes(1);
    expect(mRs.create).toBeCalledWith(
      {
        app: 'test',
        id: '1',
        ip: 'NA',
        ttl: 3600,
        d: {
          data: 'teresa teng',
        },
      },
      expect.any(Function)
    );
  });
});

单元测试结果:

 PASS  examples/67220364/index.test.ts (8.636 s)
  67220364
    ✓ should create redis connection (5 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |      70 |    57.14 |      75 |      70 |                   
 index.ts |      70 |    57.14 |      75 |      70 | 24-33             
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        9.941 s

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2019-01-16
    • 1970-01-01
    • 2021-01-01
    • 2017-08-04
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多