Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何使用Jest在TypeForm中存根EntityManager和连接_Typescript_Unit Testing_Jestjs_Nestjs_Typeorm - Fatal编程技术网

Typescript 如何使用Jest在TypeForm中存根EntityManager和连接

Typescript 如何使用Jest在TypeForm中存根EntityManager和连接,typescript,unit-testing,jestjs,nestjs,typeorm,Typescript,Unit Testing,Jestjs,Nestjs,Typeorm,我在NestJS上用Typescript开发了一个应用程序,使用TypeORM和用Jest编写的单元测试。我有一个使用如下事务的函数: async createMany(用户:用户[]){ 等待此.connection.transaction(异步管理器=>{ 等待管理员保存(用户[0]); 等待manager.save(用户[1]); }); } 这是NestJS文档中的一个示例。通过this.connection.transaction我大致上也是这样做的,但业务逻辑不同 问题是我想做一个

我在NestJS上用Typescript开发了一个应用程序,使用TypeORM和用Jest编写的单元测试。我有一个使用如下事务的函数:

async createMany(用户:用户[]){
等待此.connection.transaction(异步管理器=>{
等待管理员保存(用户[0]);
等待manager.save(用户[1]);
});
}
这是NestJS文档中的一个示例。通过
this.connection.transaction
我大致上也是这样做的,但业务逻辑不同

问题是我想做一个单元测试来测试这个服务功能。因此,我需要以某种方式模拟
这个.connection
及其
管理器
。或者至少是经理。我不知道如何用笑话来表达。我无法创建没有连接的管理器。我无法在没有管理器返回的情况下创建模拟连接

在NestJS中,同时使用TypeORM和Jest是标准的。必须有一种使用事务编写单元测试的方法。但我不知道怎么做


注意,我询问的是单元测试模拟ORM。不是直接使用测试db实例的集成测试。

您需要使用模拟的
连接创建一个
TestingModule
。像这样的事情:

import { Test } from "@nestjs/testing";

describe("UsersService", () => {
    let usersService;
    let connection;
    const mockConnection = () => ({
        transaction: jest.fn()
    });

    beforeEach(async () => {
        const module = await Test.createTestingModule({
            providers: [
                UsersService,
                {
                    provide: Connection,
                    useFactory: mockConnection
                }
            ],
        }).compile();

        usersService = await module.get<UsersService>(UsersService); 
        connection = await modle.get<Connection>(Connection);
    });

    describe("some tests", () => {
        it("should test something", async () => {
            const someMockedUsers = [/* some users */];
            const mockedManager = {
                save: jest.fn()
            }
            connection.transaction.mockImplementation((cb) => {
                cb(mockedManager);
            });

            await userService.createMany(someMockedUsers);

            expect(connection.transaction).toHaveBeenCalled();
            expect(mockedManager.save).toHaveBeenCalledTimes(2);
            // ...

        });
    });


});
从“@nestjs/testing”导入{Test};
描述(“用户服务”,()=>{
让用户服务;
让连接;
常量mockConnection=()=>({
事务:jest.fn()
});
beforeach(异步()=>{
const module=await Test.createTestingModule({
供应商:[
用户服务,
{
提供:连接,
useFactory:mockConnection
}
],
}).compile();
usersService=wait module.get(usersService);
连接=等待模式获取(连接);
});
描述(“一些测试”,()=>{
它(“应该测试一些东西”,异步()=>{
const someMockedUsers=[/*一些用户*/];
常量mockedManager={
保存:jest.fn()
}
connection.transaction.mockImplementation((cb)=>{
cb(模拟经理);
});
wait userService.createMany(someMockedUsers);
expect(connection.transaction).toHaveBeenCalled();
预期(mockedManager.save).已被调用时间(2);
// ...
});
});
});

我相信您也可以:

import { getConnectionToken } from '@nestjs/typeorm';

{
  provide: getConnectionToken(),
  useClass: Connection,
},

这应该行得通。然而,使用
Test.createTestingModule
造成了很多占用RAM负载的问题。我最终没有使用它,而是选择了
sinon.js
。@kkkk您的解决方案非常有效,谢谢!有一些我没有得到的东西,当我期望调用连接时,我得到了一个错误。尽管mock的调用是正确的。你知道jest为什么不录电话吗?