用于在nestjs中测试的redis mock

x3naxklr  于 2021-06-10  发布在  Redis
关注(0)|答案(2)|浏览(550)

我正在email-service.spec.ts文件中编写测试用例
我的电子邮件服务文件

@Injectable()
export class EmailSubscriptionService {
  private nodeTokenCache;
  private result;
  constructor(@InjectRepository(ConsumerEmailSubscriptions) private readonly emailSubscriptions: Repository<ConsumerEmailSubscriptions>,
    @InjectRepository(EmailSubscriptions) private readonly emailSubscriptionLegacy: Repository<EmailSubscriptions>,
    @InjectRedisClient('0') private redisClient: Redis.Redis,
     private readonly config: ConfigService, private http: HttpService,
    private readonly manageSavedSearchService: ManageSavedSearchService) {
  }

我的email-service.spec.ts文件

import { RedisService } from 'nestjs-redis';
import { ConfigService } from '@nestjs/config';
import { HttpService } from '@nestjs/common';
import { ManageSavedSearchService } from './../manage-saved-search/manage-saved-search.service';

describe('EmailSubscriptionService', () => {
  let service: EmailSubscriptionService;
  let entity : ConsumerEmailSubscriptions;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports:[RedisModule],

      // https://github.com/nestjs/nest/issues/1229
      providers: [EmailSubscriptionService,
        {
          // how you provide the injection token in a test instance
          provide: getRepositoryToken(ConsumerEmailSubscriptions),
          // as a class value, Repository needs no generics
          useClass: Repository,
          // useValue: {

          // }
        }, 
        {
          provide: getRepositoryToken(EmailSubscriptions),
          useClass: Repository,
        },
        RedisService,
        // {
        //   provide : RedisService,
        //   useClass: Redis
        // },

       ConfigService, HttpService, ManageSavedSearchService

      ],
    }).compile();

    service = module.get<EmailSubscriptionService>(EmailSubscriptionService);
    // entity = module.get<Repository<ConsumerEmailSubscriptions>>(getRepositoryToken(ConsumerEmailSubscriptions));

  });

  it('should be defined', async () => {
    expect(service).toBeDefined;
  });
});

结果---->nest无法解析emailsubscriptionservice(ConsumerMailSubscriptionsRepository、emailsubscriptionsrepository、?configservice、httpservice、managesavedsearchservice)的依赖项。请确保索引[2]处的参数redis\u client\u provider\u 0在roottestmodule上下文中可用。
根据服务文件中的依赖关系,我无法在email-service.spec.ts中模拟我的redisclient。我尝试过useclass,在provide中添加了redisservice,但是没有get redis方法。我能够模仿存储库和服务,我不确定,因为我被redis困住了。
如果你知道如何模仿redis,在文档里找不到任何东西。同样在下一步中,导入服务会起作用还是我必须做其他事情?configservice、httpservice、managesavedsearchservice:managesavedsearchservice

nhhxz33t

nhhxz33t1#

如果您想在测试中模拟redisclient,您需要 provide 和你从那里得到的一样 @InjectRedisClient('0') . 这将允许您为测试目的替换redis客户机。
我不熟悉您正在使用的特定redis库,但是假设是这个库,您可以看看令牌是如何构造的

r6hnlfcb

r6hnlfcb2#

beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [EmailSubscriptionService,{
        provide: EmailSubscriptionService,
        useValue: {
          getClient: jest.fn(),
        }
      }],
    }).compile();

    service = module.get<EmailSubscriptionService>(EmailSubscriptionService);
  });

这似乎在某种程度上起作用,服务类构造函数使用了许多其他类,但是在provide works中使用它。这有点违反直觉,因为构造函数中的类需要单独模拟,但不这样做它就可以工作。

相关问题