如何解决开玩笑嘲笑服务中的mongodb记录
我正在尝试在Nestjs中测试一项服务,该服务负责使用Jest从mongo数据库中获取一条记录。
按照通用约定,在编写用于测试服务的单元测试时,我们可以模拟一条记录,该记录位于数据库中。
我正在尝试以下实现:
import { Test } from '@nestjs/testing';
import { QuestionsService } from './questions.service';
import { CreateQuestionRequestDto } from './dto/create-question-request.dto';
import { getModelToken } from '@nestjs/mongoose';
import { UpdateQuestionRequestDto } from './dto/update-question-request.dto';
import { NotFoundException } from '@nestjs/common';
import { DuplicateQuestionRequestDto } from './dto/duplicate-question-request.dto';
const testQuestion: CreateQuestionRequestDto = {
data: {
createdBy: { id: 0,name: '' },lanugageTexts: undefined,options: undefined,status: undefined,type: undefined,entityId: 1,propertyId: 'propHash1',companyId: 1,entityType: 'announcement',},};
describe('QuestionsService',() => {
let questionService: QuestionsService;
let findOne: jest.Mock;
let findOneAndUpdate: jest.Mock;
let find: jest.Mock;
beforeEach(async () => {
// save = jest.fn();
findOne = jest.fn();
findOneAndUpdate = jest.fn();
find = jest.fn();
const module = await Test.createTestingModule({
providers: [
QuestionsService,{
provide: getModelToken('Question'),useValue: {}
}
]
})
.compile();
questionService = await module.get<QuestionsService>(QuestionsService);
});
it('should be defined',() => {
expect(questionService).toBeDefined();
});
/**
* Question Get
*/
describe('when getting a question',() => {
describe('and the questionId does not exist',() => {
beforeEach(() => {
findOne.mockReturnValue(undefined);
})
it('should throw a NotFound exception',async () => {
const response = await questionService.get('announcement',9136500000);
expect(response).toThrow(NotFoundException);
});
});
describe('and the questionId exists',() => {
beforeEach(() => {
findOne.mockResolvedValue(Promise.resolve(testQuestion));
});
it('should update the correct question',async() => {
const response = await questionService.get('announcement',1);
expect(response).toMatchObject(updatedTestQuestion);
});
});
});
});
运行此测试时,出现以下错误消息。
● QuestionsService › when getting a question › and the questionId does not exist › should throw a NotFound exception
TypeError: this.questionModel.find is not a function
52 | const data: Question[] = [];
53 | const questions = await this.questionModel
> 54 | .find(
| ^
55 | { entityType: entityType,entityId: entityId,status: QuestionStatus.ACTIVE },56 | { answers: 0 },57 | {
at QuestionsService.get (questions/questions.service.ts:54:14)
at Object.<anonymous> (questions/questions.spec.ts:128:56)
我正在测试的服务方法是
async get(entityType: string,entityId: number): Promise<any> {
const data: Question[] = [];
const questions = await this.questionModel
.find(
{ entityType: entityType,{ answers: 0 },{
sort: { _id: -1 },limit: 1,}
)
.exec();
if (!questions.length) {
throw new NotFoundException();
}
questions.forEach((question) => {
data.push(question);
});
return { data };
}
find()是从数据库中获取记录的猫鼬方法。我相信对于测试,我需要以某种方式包括我正在服务中使用的这些方法并对其进行模拟,但是我找不到一个明确的答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。