#Testing with mongodb

1 messages · Page 1 of 1 (latest)

lucid nova
#

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);
    }
}
#

and my unit test:

describe('UserController', () => {
    let userController: UserController;
    let userService: UserService;

    beforeEach(async () => {
        const moduleRef = await Test.createTestingModule({
            controllers: [UserController],
            providers: [UserService]
        }).compile();

        userService = moduleRef.get<UserService>(UserService);
        userController = moduleRef.get<UserController>(UserController);
    });

    it('should return a new user', () => {
        const payload: User = {
            type: "user",
            name: "Lorraine Baines",
            email: "[email protected]",
            phone: "(410) 846-3762",
            photo_avatar: "https://kgo.googleusercontent.com/profile_vrt_raw_bytes_1587515358_10512.png",
            reg_token: "1234-2121-1221-1211"
        }
        expect(userController.writeUser(payload)).toEqual({
            ...payload,
            _id: expect.any(mongoose.Types.ObjectId),
        })
    })
});
#

And i have this error: Nest can't resolve dependencies of the UserService (?, AppointmentService). Please make sure that the argument UserModel at index [0] is available in the RootTestModule context

#

How can i execute test with mongodb?

torn thorn
#

You should mock out what isn't directly being tested e.g. you're testing the users controller so you should mock the users service with a custom provider.

This repo has a lot of examples https://GitHub.com/jmcdo29/testing-nestjs

lucid nova
#

Thanks for reply, i'll check this