Nest 无法解析 JokesService 的依赖项(?)
Nest can't resolve dependencies of the JokesService (?)
我正在学习 NestJS,我对这个“模块”的工作方式有点困惑。我只有两个模块,JokesModule
和ChuckNorrisApiModule
,我正在尝试在JokesService
中使用ChukNorrisService
的服务。
我已经在导出 ChuckNorrisService
并将 ChuckNorrisModule
导入 JokesModule
.
它 运行 很好,编译器不会抛出任何错误,但是当我 运行 测试时(nest g service
和 nest g module
附带) , 它失败了。
模块:
@Module({
providers: [ChuckNorrisApiService],
imports: [HttpModule],
exports: [ChuckNorrisApiService]
})
export class ChuckNorrisApiModule {}
@Module({
providers: [JokesService],
imports: [ChuckNorrisApiModule]
})
export class JokesModule {}
以及服务:
@Injectable()
export class ChuckNorrisApiService {
constructor(private httpService: HttpService) {}
}
@Injectable()
export class JokesService {
constructor(
private readonly service: ChuckNorrisApiService,
) {}
}
测试:
describe('JokesService', () => {
let service: JokesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [JokesService],
}).compile();
service = module.get<JokesService>(JokesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
错误:
Nest cant resolve dependencies of the JokesService (?). Please make sure that the
argument ChuckNorrisApiService at index [0] is available in the RootTestModule context.
Potential solutions:
- If ChuckNorrisApiService is a provider, is it part of the current RootTestModule?
- If ChuckNorrisApiService is exported from a separate @Module, is that module imported within RootTestModule?
@Module({
imports: [ /* the Module containing ChuckNorrisApiService */ ]
})
请问 NestJS 的专家,如果我只想在其他服务中使用 ChuckNorrisApiService
,我是否需要为此创建一个完整的模块?这方面的最佳做法是什么?
如果有人知道为什么会发生这种情况或上述问题的答案,请告诉我!谢谢。
如果要进行单元测试,则需要在 RootTestModule 中导入所需的模块并模拟依赖项。
我正在学习 NestJS,我对这个“模块”的工作方式有点困惑。我只有两个模块,JokesModule
和ChuckNorrisApiModule
,我正在尝试在JokesService
中使用ChukNorrisService
的服务。
我已经在导出 ChuckNorrisService
并将 ChuckNorrisModule
导入 JokesModule
.
它 运行 很好,编译器不会抛出任何错误,但是当我 运行 测试时(nest g service
和 nest g module
附带) , 它失败了。
模块:
@Module({
providers: [ChuckNorrisApiService],
imports: [HttpModule],
exports: [ChuckNorrisApiService]
})
export class ChuckNorrisApiModule {}
@Module({
providers: [JokesService],
imports: [ChuckNorrisApiModule]
})
export class JokesModule {}
以及服务:
@Injectable()
export class ChuckNorrisApiService {
constructor(private httpService: HttpService) {}
}
@Injectable()
export class JokesService {
constructor(
private readonly service: ChuckNorrisApiService,
) {}
}
测试:
describe('JokesService', () => {
let service: JokesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [JokesService],
}).compile();
service = module.get<JokesService>(JokesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
错误:
Nest cant resolve dependencies of the JokesService (?). Please make sure that the
argument ChuckNorrisApiService at index [0] is available in the RootTestModule context.
Potential solutions:
- If ChuckNorrisApiService is a provider, is it part of the current RootTestModule?
- If ChuckNorrisApiService is exported from a separate @Module, is that module imported within RootTestModule?
@Module({
imports: [ /* the Module containing ChuckNorrisApiService */ ]
})
请问 NestJS 的专家,如果我只想在其他服务中使用 ChuckNorrisApiService
,我是否需要为此创建一个完整的模块?这方面的最佳做法是什么?
如果有人知道为什么会发生这种情况或上述问题的答案,请告诉我!谢谢。
如果要进行单元测试,则需要在 RootTestModule 中导入所需的模块并模拟依赖项。