首页 > 解决方案 > 开玩笑测试模拟 200

问题描述

嗨,我需要测试一个登录控制器,但总是 400,我需要模拟 200,但老实说,我不知道该怎么做,这里是我的测试:

test("JWT OK, status 200, remember false", async () => {


   const code = "code";
  const state = "state";
  const company = "comany";

  const response = await supertest(app)
    
     .get(`/auth/${company}`)
    .send({
      "state": state,
      "code": code
   
    }) 
  
  
});

这里是我的代码控制器登录名,来自 (/auth/${company}`)

export class AuthService {
    public async Login(req: Request, res: Response): Promise<Response<any> | undefined> {
        try {
            const company: IAuth['company']  = req.params.company;
            const state : IAuth['state'] = req.query.state;
            const code : IAuth['code'] = req.query.code;  
          ....some code....

问题始终是 response.status 400,我不知道如何测试模拟状态 200 ...

对不起,我刚开始在这个领域..

标签: typescriptjestjs

解决方案


如您所见,您的函数从请求的查询中Login读取。state, code但是,在您的测试中,您将它们作为请求正文发送。然后可能state, code会丢失然后服务器抛出400。

我们试试看

...
  const response = await supertest(app)
    .get(`/auth/${company}`)
    .query({ // query instead of send
      "state": state,
      "code": code
    }) 
...

推荐阅读