Skip to content

Commit

Permalink
fix: 修改单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Liang-Yaxin committed Oct 10, 2024
1 parent 0799daa commit 796350f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
38 changes: 26 additions & 12 deletions server/src/modules/survey/__test/surveyGroup.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SurveyGroupController } from '../controllers/surveyGroup.controller';
import { SurveyGroupService } from '../services/surveyGroup.service';
import { HttpException } from '@nestjs/common';
import { SurveyMetaService } from '../services/surveyMeta.service';
import { HttpException } from 'src/exceptions/httpException';
import { ObjectId } from 'mongodb';
import { Logger } from 'src/logger';

jest.mock('src/guards/authentication.guard');

describe('SurveyGroupController', () => {
let controller: SurveyGroupController;
let service: SurveyGroupService;
let surveyMetaService: SurveyMetaService;

Check failure on line 14 in server/src/modules/survey/__test/surveyGroup.controller.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

'surveyMetaService' is defined but never used

const mockService = {
create: jest.fn(),
Expand All @@ -19,10 +24,23 @@ describe('SurveyGroupController', () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SurveyGroupController],
providers: [
{
provide: SurveyMetaService,
useValue: {
countSurveyMetaByGroupId: jest.fn().mockResolvedValue(0),
},
},
{
provide: SurveyGroupService,
useValue: mockService,
},
{
provide: Logger,
useValue: {
error: jest.fn(),
info: jest.fn(),
},
},
],
}).compile();

Expand Down Expand Up @@ -63,37 +81,33 @@ describe('SurveyGroupController', () => {

describe('findAll', () => {
it('should return a list of survey groups', async () => {
const result = { total: 1, list: [], allList: [] };
const result = { total: 0, list: [], allList: [] };
jest.spyOn(service, 'findAll').mockResolvedValue(result);
const mockReq = { user: { _id: new ObjectId() } };
const mockQue = { curPage: 1, pageSize: 10, name: '' }
const userId = mockReq.user._id.toString();
expect(await controller.findAll(mockReq, mockQue)).toBe(result);
expect(await controller.findAll(mockReq, mockQue)).toBe({ code: 200, data: result });
expect(service.findAll).toHaveBeenCalledWith(userId, '', 0, 10);
});
});

describe('update', () => {
it('should update a survey group', async () => {
const updatedFields = { raw: '', generatedMaps: [] };
const updatedFieldOne = { name: 'update a survey group' };
const updatedFields = { name: 'xxx' }
const updatedResult = { raw: 'xxx', generatedMaps: [] }
const id = '1';
jest.spyOn(service, 'update').mockResolvedValue(updatedFields);
jest.spyOn(service, 'update').mockResolvedValue(updatedResult);

expect(await controller.updateOne(id, updatedFieldOne)).toEqual({
expect(await controller.updateOne(id, updatedFields)).toEqual({
code: 200,
ret: updatedFields,
ret: updatedResult,
});
expect(service.update).toHaveBeenCalledWith(id, updatedFields);
});

it('should throw error on invalid parameter', async () => {
const id = '1';
const invalidFields: any = {};
jest.spyOn(service, 'update').mockImplementation(() => {
throw new HttpException('参数错误', 400);
});

await expect(controller.updateOne(id, invalidFields)).rejects.toThrow(
HttpException,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('SurveyMetaController', () => {
survey: {
title: reqBody.title,
remark: reqBody.remark,
groupId: null,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('SurveyMetaService', () => {
createMethod: params.createMethod,
createFrom: params.createFrom,
workspaceId: params.workspaceId,
groupId: null,
});
expect(surveyRepository.save).toHaveBeenCalledWith(newSurvey);
expect(result).toEqual(newSurvey);
Expand Down

0 comments on commit 796350f

Please sign in to comment.