我正在我的 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 的依赖项(ConsumerEmailSubscriptionsRepository、EmailSubscriptionsRepository、?、ConfigService、HttpService、ManageSavedSearchService)。请确保索引 [2] 处的参数 REDIS_CLIENT_PROVIDER_0 在 RootTestModule 上下文中可用。
我无法根据服务文件中的依赖关系在 email-service.spec.ts 中模拟我的 redisclient。我试过useClass,在provide中添加了RedisService,没有get-redis方法。我能够模拟存储库和服务,我不确定,因为我被 redis 困住了。
任何想法如何模拟 redis,在文档中都找不到任何东西。同样在下一步中,导入服务会起作用还是我必须做其他事情?ConfigService、HttpService、manageSavedSearchService:ManageSavedSearchService