用ts-mockito模拟ioredis失败

时间:2019-03-12 08:51:03

标签: mockito inversifyjs ioredis

我正在通过 inversify 工厂将自定义类以及 ioredis 注入我的类中:

import { inject } from 'inversify';
import * as Redis from 'ioredis';
import { MyOtherClass } from './MyOtherClass';

export class MyClass {
  private redis!: Redis.Redis;
  private myOther!: MyOtherClass;

  constructor(@inject(TYPES.MYOTHERCLASS) private readonly myOtherFactory: () => Promise<MyOtherClass>,
              @inject(TYPES.REDIS_FACTORY) private readonly redisFactory: () => Promise<Redis.Redis>) {
  }

  public async init(): Promise<void> {
     this.myOther = await this.myOtherFactory();
     console.log('a');

     this.redis = await this.redisFactory();
     console.log('b');
  }
}

我正在测试文件中:

import 'mocha';
import { instance, mock } from 'ts-mockito';
import * as Redis from 'ioredis';
import { MyClass, MyOtherClass } from '../src';

describy('MyClass', () => {
  let myClass: MyClass;

  const mockedMyOther = mock(MyOtherClass);
  const myOtherFactory = () => Promise.resolve(instance(mockedMyOther));

  const mockedRedis = mock(Redis);
  const redisFactory = () => Promise.resolve(instance(mockedRedis));

  beforeEach( async () => {
    myClass = new MyClass(myOtherFactory, redisFactory);
    await myClass.init();
  });

  it('should init correctly');
});

运行测试时,它仅输出a并在执行redisFactory时超时。调试会话会在相同位置立即(静默地)终止。
没有其他信息可以提示我出了什么问题。

有什么想法吗?

0 个答案:

没有答案