testing - 如何测试与其他模块服务关联的 Controller ?嵌套

标签 testing controller jestjs tdd nestjs

我应该如何测试一个有授权服务和用户服务的 Controller ? 我正在尝试遵循书中的 TDD 方法,但它不再像那样工作了。

有什么解决办法吗?

这是我要测试的 Controller :auth.controller.ts

import { Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { UserService } from '../user/user.service';

@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService,
  ) {}
  @Post()
  async signup() {
    throw new Error('Not Implemented!');
  }

  @Post()
  async signin() {
    throw new Error('Not Implemented Error!');
  }
}

这是一个将由 auth Controller 用来处理操作的服务:auth.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class AuthService {}

这是我需要使用的外部服务,以便查找和验证用户user.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class UserService {}

我在这里尝试为 auth Controller 做一些 TDD 测试:auth.controller.spec.ts

import { Test } from '@nestjs/testing';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { UserService } from '../user/user.service';

describe('EntriesController', () => {
  let authController: AuthController;
  let authSrv: AuthService;

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      controllers: [AuthController],
      providers: [AuthService, UserService],
    })
      .overrideProvider(AuthService)
      .useValue({ signup: () => null, signin: () => null })
      .compile();

    authController = await module.get<AuthController>(AuthController);
    authSrv = await module.get<AuthService>(AuthService);
  });

  describe('signup', () => {
    it('should add new user to the database', async () => {
      expect(await authController.signin()).toBe(true);
      console.log(authController);
    });
  });

  describe('signin', () => {
    it('should sign in user, if credentials valid', async () => {});
  });
});

最佳答案

而不是使用 overrideProvider 你应该直接在 providers 数组中使用类似这样的东西设置模拟:

beforeEach(async () => {
  const module = await Test.createTestingModule({
    controllers: [AuthController],
    providers: [
      {
        provide: AuthService,
        useValue: { signup: () => null, signin: () => null }
      },
      UserService
    ],
  })
  .compile();

  authController = await module.get<AuthController>(AuthController);
  authSrv = await module.get<AuthService>(AuthService);
});

UserService 也应该这样做,这样您就可以创建真正的单元测试,只测试直接类而忽略其余类。 This repository of mine展示了许多使用 NestJS 的项目的不同测试示例。看一看可能会有所帮助。

关于testing - 如何测试与其他模块服务关联的 Controller ?嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59353236/

相关文章:

performance - 如何验证我的 LoadRunner 测试脚本是否确实在执行?

.net - SSL 和 ASP.NET MVC

javascript - Jest : ReferenceError: describe is not defined

unit-testing - 使用 jest 测试 nuxt.js 应用程序时访问 `process.env` 属性

javascript - 将 Jest 文本覆盖记者输出到 .txt

python - Python模拟中的模拟属性?

python - 如何使用 pytest-xdist 访问共享资源?

java - JUnit - 字符串检查失败,因为\n

java - 找不到@Resource对象在Spring xml中实例化的位置

java - responseEntity 的问题。即使 ResponseEntity.HttpStatus 为 201,MockMvc.Perform() 也会给出 200 状态代码