Hello!
I have to write a unit tests for my service:
@Injectable()
export class UserService {
constructor(
@InjectModel(User.name) private userModel: Model<UserDocument>,
@Inject(forwardRef(() => AppointmentService))
private appointmentService: AppointmentService
) {}
async writeUser(payload: User) {
return (await this.userModel.create(payload)).toObject({ versionKey: false });
}
async getById(id: string) {
console.log(id)
const a = await this.userModel.findById(new mongoose.Types.ObjectId(id));
if (!a) {
throw new UserException(id);
}
return a;
}
async getAll() {
return this.userModel.find();
}
async getAppt(id: string) {
return this.appointmentService.getByUserId(id);
}
}
Controller :
@Controller('user')
export class UserController {
constructor(private userService: UserService) {
}
@Post('create')
async writeUser(@Body() user) {
return await this.userService.writeUser(user);
}
@Get('get')
async getAll() {
return await this.userService.getAll();
}
@Get('getUser/:id')
@UsePipes(IdValidationPipe)
async getOne(@Param('id') id: string) {
return await this.userService.getById(id);
}
@Get('getAppts/:id')
@UsePipes(IdValidationPipe)
async getAppt(@Param('id') id: string) {
return await this.userService.getAppt(id);
}
}