我正在通过Supertest
向我的开发服务器发送请求进行集成测试。但是我在如何将数据放入数据库方面遇到了困难。例如,在运行GET测试之前,我想将数据插入到数据库中。但是我甚至无法从TypeORM获取连接:
ConnectionNotFoundError: Connection "default" was not found.
如果我能够从 TypeORM 获取到连接,那么如何在事务中包装测试,并在测试完成后回滚事务,以确保集成测试不会影响真实数据库。
基本上,是否有类似于 Rails 的 factory_bot
的包?
describe("Templates", (): void => {
describe("GET /api/v1/templates/{templateHash}", (): void => {
let template: Template;
beforeEach(
async (): Promise<void> => {
let templateService: TemplateService = new TemplateService();
let connection: Connection = getConnection("default");
template = new Template(Buffer.from(faker.random.words()), faker.random.uuid(), faker.system.fileName());
await templateService.create(connection, template);
},
);
it("should return a template", (done): void => {
request(config.devEnvEndpoint)
.get(`api/v1/templates/${template.templateHash}`)
.set("Accept", "application/json")
.expect(200)
.end((err, response): void => {
console.log(response);
done();
});
});
});
});