【问题标题】:jest test simulate 200开玩笑测试模拟 200
【发布时间】:2021-06-16 07:49:12
【问题描述】:

您好,我需要测试一个登录控制器,但总是 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...

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

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

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

    我们试试

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

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 2018-06-03
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多