【问题标题】:I need to run this test of my NestJs application我需要运行我的 NestJs 应用程序的测试
【发布时间】:2021-11-02 13:58:28
【问题描述】:

我正在尝试为一个简单的功能做这个测试,但我做不到。我发布了我的测试代码和错误。

我尝试了几种不同的方法,但都没有成功。 我正在使用 NestJS CLI 和使用 jestJs 的测试

 // My coding
  
  createSession(login: string, password: string) {
    const search: UserEntity = this.users.find(
      (user: UserEntity) => user.cpf === login,
    );
    if (search) return search.senha === password;
    else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
  }
  
// My test
  
  it('should retrieve getHello', async () => {
    await expect(
        service.createSession(mockLogin.login, mockLogin.password),
    ).rejects.toEqual(
      new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED),
    );
  });
  
  
  // The error
  
   ● LoginService › Get service › should retrieve getHello

    HttpException: UNAUTHORIZED

      22 |     );
      23 |     if (search) return search.senha === password;
    > 24 |     else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
         |                ^
      25 |   }
      26 | }
      27 |

【问题讨论】:

  • 请不要使用代码图像,而是使用降价格式的代码块。代码图像不可分割,使问题更难阅读和诊断
  • 做一些调试。例如:将console.log(this.users) 添加到您的createSession 方法中
  • 代码没问题,我有用户反馈,我的问题是要模拟HttpException。

标签: jestjs nestjs


【解决方案1】:

我设法解决了它:

    it('should retrieve', async () => {
      await expect(
        service.createSession(mockLogin.login, mockLogin.password),
      ).rejects.toThrow();
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多