mpa*_*lma 7 javascript node.js nestjs
我有一些服务,我在构造函数中注入 CACHE_MANAGER
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
...
export class ManagerService {
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
...
}
Run Code Online (Sandbox Code Playgroud)
当我测试导入这些服务的模块时,这给了我一个错误
Nest can't resolve dependencies of the ManagerService (?). Please make sure that the argument CACHE_MANAGER at index [0] is available in the Web3ManagerService context.
我对 NestJs 比较陌生,所以我真的不知道如何解决它
小智 13
我刚刚遇到了同样的问题并以这种方式解决了:
经理.服务.规格.ts
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ManagerService, { provide: CACHE_MANAGER, useValue: {} }],
}).compile();
service = module.get<ManagerService>(ManagerService);
});
Run Code Online (Sandbox Code Playgroud)
要在 下注入缓存管理器提供程序CACHE_MANAGER,您需要将创建此提供程序的 Nestjs 模块导入到具有以下内容的模块中:ManagerService
@Module({
imports: [CacheModule.register()], // <<<<
providers: [ManagerService],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
就像文档显示的那样https://docs.nestjs.com/techniques/caching
| 归档时间: |
|
| 查看次数: |
10866 次 |
| 最近记录: |