gpt4 book ai didi

node.js - 如何用 Jest 在服务(NestJS)中测试模型(Mongoose)

转载 作者:行者123 更新时间:2023-12-01 21:43:11 25 4
gpt4 key购买 nike

我有一个用 NestJS 完成的后端。在我的服务中,我注入(inject)了两个 Mongoose 模型。我使用 Jest 来测试服务。模型按原样声明并注入(inject)模块:

quizes.providers.ts
import { Connection } from 'mongoose';
import { QuizSchema } from './schemas/quiz.schema';

export const quizesProviders = [
{
provide: 'CLASS_MODEL',
useFactory: (connection: Connection) => connection.model('Quiz', QuizSchema),
inject: ['DATABASE_CONNECTION'],
},
];

users.providers.ts
import { Connection } from 'mongoose';
import { UserSchema } from './schemas/user.schema';

export const usersProviders = [
{
provide: 'USER_MODEL',
useFactory: (connection: Connection) => connection.model('User', UserSchema),
inject: ['DATABASE_CONNECTION'],
},
];

模块示例:

quizes.module.ts
import { Module } from '@nestjs/common';
import { QuizesController } from './quizes.controller';
import { QuizesService } from './quizes.service';

import { quizesProviders } from './quizes.providers';

import { usersProviders } from '../auth/users.providers';

import { DatabaseModule } from 'src/database.module';
import { AuthModule } from 'src/auth/auth.module';

@Module({
imports: [DatabaseModule, AuthModule],
controllers: [QuizesController],
providers: [QuizesService,
...quizesProviders, ...usersProviders]
})
export class QuizesModule {}

然后在我的服务中,我注入(inject)模型:

quizes.service.ts
@Injectable()
export class QuizesService {
constructor(
@Inject('CLASS_MODEL')
private classModel: Model<Quiz>,
@Inject('USER_MODEL')
private userModel: Model<User>
) {}

在我的 quizes.spec.ts(开 Jest )中,我开始做那样的事情。它编译但不工作:

import { Test } from '@nestjs/testing';
import * as mongoose from 'mongoose';
import { User } from 'src/auth/user.interface';
import { Quiz } from './quiz.interface';
import { databaseProviders } from '../database.providers';

const USER_MODEL:mongoose.Model<User> = mongoose.model('User', UserSchema);

const CLASS_MODEL:mongoose.Model<Quiz> = mongoose.model('Quiz', QuizSchema);

const mockingQuizModel = () => {
find: jest.fn()
}

const mockingUserModel = () => {
find: jest.fn()
}

const mockUser = {
username: 'Test user'
}

describe('QuizesService', () => {
let quizesService;
let userModel , classModel;


beforeEach(async () => {
const module = await Test.createTestingModule({

providers: [QuizesService, ...usersProviders, ...quizesProviders,...databaseProviders,
{provide: USER_MODEL, useFactory: mockingUserModel},
{provide: CLASS_MODEL, useFactory: mockingQuizModel},
],
}).compile();

quizesService = await module.get<QuizesService>(QuizesService);
classModel = await module.get<mongoose.Model<Quiz>>(CLASS_MODEL)
userModel = await module.get<mongoose.Model<User>>(USER_MODEL)

})

describe('getAllQuizes', ()=> {

it('get all quizes', () => {
expect(userModel.find).not.toHaveBeenCalled();
})

})
})

userModel 未定义,测试不退出。

最佳答案

使用 NestJS 官方定义的 getModelToken 函数:https://docs.nestjs.com/v6/技术 -> Mongo(向下滚动到测试部分)

那么你的代码应该看起来像这样:

import { getModelToken } from '@nestjs/mongoose';

const mockRepository = {
find() {
return {};
}
};

const module = await Test.createTestingModule({
providers: [ ...,
{provide: getModelToken('CLASS_MODEL'), useValue: mockRepository,},
{provide: getModelToken('USER_MODEL'), useValue: mockRepository,},
],
...

关于node.js - 如何用 Jest 在服务(NestJS)中测试模型(Mongoose),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com