【发布时间】:2020-02-17 03:37:23
【问题描述】:
我正在通过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();
});
});
});
});
【问题讨论】:
标签: node.js typescript integration-testing typeorm