我有以下内容:
@Injectable({ scope: Scope.REQUEST })
export class RequestScopedCache extends Object {
private storage: any = {};
public set(key: string, value: any) {
this.storage[key] = value;
}
public get(key: string) {
return this.storage[key];
}
}
我想创建一个使用此RequestScopedCache
的存储库的集成测试(实际依赖项)。这样做是为了向同事展示真正的集成测试和部分集成测试可以在NestJS中轻松实现。
这样做,我发现了以下内容:
describe('EnqueuedRepository', () => {
let repo: EnqueuedRepository;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
imports: [AppModule, TypeOrmModule.forFeature([Enqueued]), CachingModule],
providers: [EnqueuedService, EnqueuedRepository],
controllers: [],
}).compile();
repo = app.get<EnqueuedRepository>(EnqueuedRepository);
});
it('should find by all', async (done) => {
const data = await repo.find();
console.log(`Got data: ${data}`);
done();
});
});
不幸的是,由于RequestScopedCache
的范围是REQUEST
,因此NestJS is不会在createTestingModule
内执行依赖项注入。如何在测试中欺骗请求上下文?