在Mocha中使用Supertest和一致的基本api URL

时间:2018-12-12 11:56:17

标签: javascript node.js supertest superagent

我正在尝试针对实际的API(而非快速应用或模拟程序)首次使用Supertest。我的理解是该语法应该起作用:

const request = require('supertest')('https://my-api.com');

describe('Obtain a new customer', function() {
  it('responds with json', function(done) {
    request
      .post()
      .send({
        customer_id: '7ewISN7dekjkdn',
        client_password: 'password123'
      })
      .set('Content-Type', 'application/json')
      .expect(200)
      .then(response => {
        console.log(response.body)
        done()
      })
  });

});

.post()中,出现Unresolved function or method post()错误。我不清楚为什么。

我知道Superagent是在后台进行利用的。

有人可以给我有关该问题的指导吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

只是缺少在方法内部设置路径。对于根,只需设置“ /”。

const request = require('supertest')('https://my-api.com');

describe('Obtain a new customer', function() {
  it('responds with json', function(done) {
    request
      .post('/')
      .send({
        customer_id: '7ewISN7dekjkdn',
        client_password: 'password123'
      })
      .set('Content-Type', 'application/json')
      .expect(200)
      .then(response => {
        console.log(response.body)
        done()
      })
  });

});