#testing

6 messages · Page 1 of 1 (latest)

slow mango
#

Hello,
I'm doing unit tests for the first time with NestJs,
I just want to know if I'm on the right track

#

user.service:

#
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Request } from 'express';
import * as fs from 'fs';
import * as path from 'path';
import 'dotenv/config';
import { v4 as uuidv4 } from 'uuid';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';
import { UserWithProfilePicture } from './entities/user-pp.interface';
import { Candidate } from '../candidate/entities/candidate.entity';
import { Prescriptor } from '../prescriptor/entities/prescriptor.entity';
import { Manager } from '../manager/entity/manager.entity';
import { Role } from './entities/role.enum';

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User)
        private userRepository: Repository<User>,

        @InjectRepository(Candidate)
        private candidateRepository: Repository<Candidate>,

        @InjectRepository(Prescriptor)
        private prescriptorRepository: Repository<Prescriptor>,

        @InjectRepository(Manager)
        private managerRepository: Repository<Manager>,
    ) {}

    async create(createUserDto: CreateUserDto): Promise<User> {
        const user = this.generateUser(createUserDto);
        return await this.userRepository.save(user);
    }
    
    [...]
}```
#

create-user.dto:

import { IsDateString, IsEmail, IsNotEmpty, IsOptional, IsString, Length } from 'class-validator';

export class CreateUserDto {
    @IsNotEmpty()
    @IsString()
    @IsEmail()
    @Length(2, 255)
        email: string;

    @IsNotEmpty()
    @IsString()
    @Length(2, 255)
        firstname: string;

    @IsNotEmpty()
    @IsString()
    @Length(2, 30)
        lastname: string;

    @IsNotEmpty()
    @IsString()
    @Length(5, 255)
        password: string;

    @IsOptional()
    @IsString()
    @IsDateString()   
        birthday: string;

    @IsOptional()
    @IsString()
    @Length(0, 255)
        phone: string;

    @IsOptional()
    @IsString()
    @Length(0, 255)
        address: string;
    
    @IsOptional()
    @IsString()
    @Length(0, 255)
        postal_code: string;
}
#

Je ne test pour le moment que la méthode create