Unit testing NetsJs控制器单元测试返回TypeError:无法读取未定义的属性
试图在NetsJs控制器上写入单元测试,但获取返回类型错误:无法读取未定义的属性“getGroup” 组.controller.spec.tsUnit testing NetsJs控制器单元测试返回TypeError:无法读取未定义的属性,unit-testing,jestjs,nestjs,typeorm,nestjs-config,Unit Testing,Jestjs,Nestjs,Typeorm,Nestjs Config,试图在NetsJs控制器上写入单元测试,但获取返回类型错误:无法读取未定义的属性“getGroup” 组.controller.spec.ts describe("GroupsController", () => { let groupsController: GroupsController; let groupsService: GroupService; beforeEach(async () => { const module: Te
describe("GroupsController", () => {
let groupsController: GroupsController;
let groupsService: GroupService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GroupsController],
providers: [
{
provide: getRepositoryToken(Groups),
useFactory: GroupModelMockFactory
},
{
provide: getRepositoryToken(Roles),
useFactory: roleModelMockFactory
},
RSTLogger,
RolesService,
GroupService,
CommonService,
DatabaseService,
GroupsController
]
}).compile();
groupsService = module.get<GroupService>(GroupService);
groupsController = module.get<GroupsController>(GroupsController);
});
it("should be defined", () => {
expect(groupsController).toBeDefined();
});
describe("Find Group", () => {
it("Get group", async () => {
try {
const expectedResult = {
groupName: "group123",
description: "Group Description",
roleId: 1
};
expect(
await groupsController.getGroup(1, 'no')
).toEqual({ data: expectedResult });
} catch (e) {
console.log(e);
}
});
});
});
groups.controller.ts
@ApiTags('groups')
@ApiBearerAuth()
@Controller('groups')
@Injectable({ scope: Scope.REQUEST })
export class GroupsController {
constructor(private readonly groupsService: GroupService,
private rstLogger: RSTLogger,
private commonService: CommonService,
@Inject(REQUEST) private readonly request: Request) {
this.rstLogger.setContext('GroupController');
}
@Get(':groupId')
@ApiQuery({ name: 'groupId' })
@ApiQuery({ name: 'relatinalData' , enum: ['yes', 'no']})
@UseGuards(JWTTokenAuthGuard, RolesGuard)
async gerGroup(@Query('groupId') groupId, @Query('relatinalData') relatinalFlag) {
const group = await this.groupsService.getGroup(groupId, relatinalFlag);
if (!group) throw new NotFoundException('Group does not exist!');
return { data: group };
}
}
群服务
export class GroupService {
constructor(
@InjectRepository(Groups) private readonly groupModel: Repository<Groups>,
private rstLogger: RSTLogger,
private readonly databaseService: DatabaseService,
private readonly rolesService: RolesService) {
this.rstLogger.setContext('GroupServices');
}
async getGroup(ID, relatinalFlag = 'no'): Promise<Groups> {
if (relatinalFlag && relatinalData.yes === relatinalFlag) {
return await this.databaseService.findOneWithRelation(this.groupModel, { id: ID }, { relations: ['role'] });
} else {
return await this.databaseService.findOne(this.groupModel, { id: ID });
}
}
}
有谁能告诉我这里需要更正什么。似乎
GroupService
的依赖项有自己的依赖项。如果它们对这个测试不重要,你可以这样模仿它们
{provide:RSTLogger,useValue:jest.fn()},
但是正如我所看到的,
DatabaseService
有它自己的依赖项,应该正确地提供或模拟它。是的,但这里的问题是groupsController对象在我试图打印它时返回空对象{}。通常这意味着一些groupsController
的依赖项丢失,对象无法启动。研究RSTLogger
,DatabaseService
,RolesService
的依赖关系,您会发现其中一些在TestingModuleThaks的提供者部分遗漏了,我发现了它的组控制器依赖关系@Inject(请求)的问题。我嘲笑它,它现在工作得很好。
export class GroupService {
constructor(
@InjectRepository(Groups) private readonly groupModel: Repository<Groups>,
private rstLogger: RSTLogger,
private readonly databaseService: DatabaseService,
private readonly rolesService: RolesService) {
this.rstLogger.setContext('GroupServices');
}
async getGroup(ID, relatinalFlag = 'no'): Promise<Groups> {
if (relatinalFlag && relatinalData.yes === relatinalFlag) {
return await this.databaseService.findOneWithRelation(this.groupModel, { id: ID }, { relations: ['role'] });
} else {
return await this.databaseService.findOne(this.groupModel, { id: ID });
}
}
}
TypeError: Cannot read property 'getGroup' of undefined