无法为用户模型创建模拟构造函数
Can't create mock constructor for user model
当我试图模拟我的用户模型来测试我的服务时,nest 控制台抛出这个 TypeError,但我不知道 'emulate'用户模型的构造函数
user.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { getModelToken } from '@nestjs/mongoose';
import { UserService } from './user.service';
import { User } from './interfaces/user.interface';
const mockUsersModel = {
save: jest.fn().mockImplementation((user) => Promise.resolve({id: Date.now(), ...user}))
};
describe('UserService', () => {
let service: UserService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UserService,
{
provide: getModelToken('User'),
useValue: mockUsersModel
}],
}).compile();
service = module.get<UserService>(UserService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should create a new user record and return it', async() => {
expect(await service.createUser({email: 'user@user.com', password:'safe-password',
online: true})).toEqual({
id: expect.any(Number),
email: 'user@user.com',
password: 'safe-password'
})
})
});
user.serive.ts
constructor(@InjectModel('User') private readonly userModel: Model<User>){}
async createUser(createUserDTO: CreateUserDTO): Promise<User>{
const newUser = new this.userModel(createUserDTO);
return newUser.save();
}
new this.userModel
应为可实例化对象(具有构造函数并可用于实例化新对象实例的对象,如 class 或构造函数)。但是,您作为 @InjectModel('User')
装饰器的参数传递的 mockUsersModel
不是可实例化的对象。因此调用 new
会引发错误。
鉴于您的用例,最好通过将构造函数分配给 mockUsersModel
来解决此问题,如下所示:
const mockUsersModel = function() {
save: jest.fn().mockImplementation((user) => Promise.resolve({id: Date.now(), ...user}))
};
当我试图模拟我的用户模型来测试我的服务时,nest 控制台抛出这个 TypeError,但我不知道 'emulate'用户模型的构造函数
user.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { getModelToken } from '@nestjs/mongoose';
import { UserService } from './user.service';
import { User } from './interfaces/user.interface';
const mockUsersModel = {
save: jest.fn().mockImplementation((user) => Promise.resolve({id: Date.now(), ...user}))
};
describe('UserService', () => {
let service: UserService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UserService,
{
provide: getModelToken('User'),
useValue: mockUsersModel
}],
}).compile();
service = module.get<UserService>(UserService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should create a new user record and return it', async() => {
expect(await service.createUser({email: 'user@user.com', password:'safe-password',
online: true})).toEqual({
id: expect.any(Number),
email: 'user@user.com',
password: 'safe-password'
})
})
});
user.serive.ts
constructor(@InjectModel('User') private readonly userModel: Model<User>){}
async createUser(createUserDTO: CreateUserDTO): Promise<User>{
const newUser = new this.userModel(createUserDTO);
return newUser.save();
}
new this.userModel
应为可实例化对象(具有构造函数并可用于实例化新对象实例的对象,如 class 或构造函数)。但是,您作为 @InjectModel('User')
装饰器的参数传递的 mockUsersModel
不是可实例化的对象。因此调用 new
会引发错误。
鉴于您的用例,最好通过将构造函数分配给 mockUsersModel
来解决此问题,如下所示:
const mockUsersModel = function() {
save: jest.fn().mockImplementation((user) => Promise.resolve({id: Date.now(), ...user}))
};