简体   繁体   English

通过 Mocha 获得对 API 请求的未定义响应

[英]Getting undefined response to API Request through Mocha

I am unable to understand that why am I getting the undefined response to my code API request through mocha.我无法理解为什么我会通过 mocha 收到对我的代码 API 请求的未定义响应。 Any leads will be appreciated Attaching the codes picture and code itself too.任何线索将不胜感激附上代码图片和代码本身。

import supertest from "supertest";
import { expect } from 'chai';
const request = 
    supertest("https://api.staging.graana.rocks/api/");
let a;

describe('Users', () => {
    it('GET /users', () => {
        request.get('city?home=true').end((err,res) => {
            expect(res.body.data).to.be.not.null;
            a = res.body.data;
            console.log(res.body.data);
            console.log(a)
            
        });
    });
});

Code's picture代码图片

I am really stuck any leads would be helpful.我真的被困住了,任何线索都会有所帮助。 Thank You in advance.先感谢您。

Try to pass query params using the query() method.尝试使用query()方法传递查询参数。
The test is described to call the /users end-point so you should change the .get() path accordingly if you need to test that path.该测试被描述为调用/users端点,因此如果您需要测试该路径,您应该相应地更改.get()路径。

describe('Users', () => {
  it('GET /users', async () => {
    const res = await request.get('/city').query({ home: true });
    expect(res.body.data).to.be.not.null;
    a = res.body.data;
    console.log(res.body.data);
    console.log(a);
  });
});

Also, change your supertest declaration to (remove the trailing / ):此外,将您的supertest声明更改为(删除尾随/ ):

const request = supertest("https://api.staging.graana.rocks/api");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM