database - 如何在端到端(e2e)测试中模拟嵌套 typeorm 数据库模块?
问题描述
全部。包括有关您的目标的详细信息:我正在尝试在 e2e 测试中模拟存储库
描述预期和实际结果:对服务器的请求将无权访问持久层。我们应该模拟连接和存储库。
我已经更新了代码,但存储库仍然没有被覆盖。也许我需要通过 Facade 提供者来实现它
你可以在这里玩代码我的代码
解决方案
在 cat.module.ts 中的 app.module.ts TypeOrmModule.forRoot() 和 TypeOrmModule.forFeature([CatEntity]) 中使用它
import { getRepositoryToken } from '@nestjs/typeorm';
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
// import modules, the modules should import the entities they deal with.
// The testing module should be barebones
imports: [
// basically looks like a unit test but goes through the HTTP motions
// if you want to include the AppModule you'll need to create a configuration
// for the database module (TypeORM) that will be accessible in a testing context
// AppModule,
CatModule,
],
})
// this is for overriding a provider that exists in a module already (such as the ProjectsModule)
.overrideProvider(getRepositoryToken(ProjectEntity))
// this is how you give the factory, value, or class to use instead
.useFactory({
factory: () => ({
create: jest.fn(() => new Promise((resolve) => resolve(cat))),
find: jest.fn(() => new Promise((resolve) => resolve([cat]))),
update: jest.fn((id, project2) => new Promise((resolve) => resolve(cat2))),
findOne: jest.fn(
({ uuid }) =>
new Promise((resolve) => {
resolve(cat);
}),
),
delete: jest.fn((uuid) => new Promise((resolve) => resolve())),
save: jest.fn(
(data) =>
new Promise((resolve) => {
// data = data.uuid === undefined ? data.uuid = uuid() : data;
resolve(data);
}),
),
}),
})
.compile();
app = module.createNestApplication();
await app.init();
});
我可以回答任何问题
推荐阅读
- excel - VBA选择所有文件
- java - 我想如何使用扫描仪读取文本文件中的文本并忽略不可用的值
- oracle - ToxiProxy 错误 - 试图以访问权限禁止的方式访问套接字
- flutter - 重构代码会影响 Flutter 应用的性能吗?
- c - 如何防止错误:这种旧式功能
- node.js - React.js useState() 没有得到任何值;
- c# - 此操作系统不支持区域设置 ID。LCID=8192
- php - 是否可以在 HTML 或 PHP 的输入框中存储变量
- android - 项目装饰未在 Gridlayout 管理器 recyclerview 中的项目中添加边距
- python - 如何通过python构建socks5代理