【发布时间】:2020-12-19 04:34:10
【问题描述】:
NestJS 在 (https://docs.nestjs.com/techniques/database#transactions) 上提供了一个示例事务代码,我现在想针对该代码创建单元测试脚本。以下是一些依赖文件:
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({type: 'text', name: 'first_name'})
firstName: string;
@Column({type: 'text', name: 'last_name'})
lastName: string;
@Column({name: 'is_active', default: true})
isActive: boolean;
}
@Injectable()
export class UsersService {
constructor(
private connection: Connection
) {}
async createMany(users: User[]): User[] {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const user1 = await queryRunner.manager.save(users[0]);
await queryRunner.commitTransaction();
return [user1];
} catch (err) {
// since we have errors lets rollback the changes we made
await queryRunner.rollbackTransaction();
} finally {
// you need to release a queryRunner which was manually instantiated
await queryRunner.release();
}
}
}
这是单元测试脚本。我即将完成,但我仍然从await queryRunner.manager.save(users[0]) 获得undefined,jest.spyOn 设置如下:
describe('UsersService', () => {
let usersService: UsersService;
let connection: Connection;
class ConnectionMock {
createQueryRunner(mode?: "master" | "slave"): QueryRunner {
const qr = {
manager: {},
} as QueryRunner;
qr.manager;
Object.assign(qr.manager, {
save: jest.fn()
});
qr.connect = jest.fn();
qr.release = jest.fn();
qr.startTransaction = jest.fn();
qr.commitTransaction = jest.fn();
qr.rollbackTransaction = jest.fn();
qr.release = jest.fn();
return qr;
}
}
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService, {
provide: Connection,
useClass: ConnectionMock,
}],
}).compile();
usersService = module.get<UsersService>(UsersService);
connection = module.get<Connection>(Connection);
});
it('should be defined', () => {
expect(usersService).toBeDefined();
});
it('should return expected results after user create', async () => {
const user1: User = { id: 1, firstName: 'First', lastName: 'User', isActive: true };
const queryRunner = connection.createQueryRunner();
// I would like to make the following spyOn work
jest.spyOn(queryRunner.manager, 'save').mockResolvedValueOnce(user1);
expect(await appService.createMany([user1])).toEqual([user1]);
});
});
【问题讨论】: