【问题标题】:How to run mocha tests synchronously, for POST and GET request如何为 POST 和 GET 请求同步运行 mocha 测试
【发布时间】:2020-12-23 07:38:28
【问题描述】:

我为 couchbase 数据库构建了一个 API,我目前正在使用 mocha 为我的 HTTP 请求编写测试。我遇到的问题是我的 GET 请求测试找不到通过 POST 请求测试添加的数据。如果在运行测试之前数据库中存储了数据,则 GET 请求测试将通过。但它返回的数据不包括 POST 请求测试中新添加的数据。我需要能够在干净的数据库上运行测试并通过所有测试。我相信这个问题是由于 mocha 的异步特性引起的,但我不确定如何解决这个问题。这是我的测试用例。

依赖关系

const expect = require('chai').expect;
const request = require('supertest');
const app = require('../src/app');

POST 请求测试

describe(`POST requests`, ()=>{
  it('Add new data', function() {
    return request(app)
      .post('/someendpoint')
      .set('Accept', 'application/json')
      .set('Authorization', auth_token)
      .send(newData)
      .expect(200)
      .then( function(res) {
        expect(res.body).be.a('Object');
        expect(res.body.id).be.a('string');
        id = res.body.id;
      })
    });
  });

GET 请求测试

describe('GET requests', function() {
  it('Get data', function() {
    return request(app)
      .get('/someendpoint')
      .set('Accept', 'application/json')
      .set('Authorization', auth_token)
      .expect(200)
      .then((res) => {
        expect(res.body).be.a('Array');
      })
    });
  });

另外,如果它有所作为。每次发出 POST 请求时,都会在 couchbase 数据库中创建一个新文档

这是我发现的一个糟糕的解决方案(编辑)

    before('timer for get data', function(){
        for(let i = 0; i < 1000000000; i++){
        }
    })
    
    it('get data', function() {
        return request(app)
        .get('/someendpoint')
        .set('Accept', 'application/json')
        .set('Authorization', auth_token)
        .expect(200)
        .then((res) => {
            expect(res.body).be.a('Array');
        });
    });

【问题讨论】:

  • 那你为什么把它分成两个测试呢?这是一项测试,包含多个步骤。
  • 因为我有我所有的测试,基于请求的类型:POST、GET、PUT、DELETE,分组在一起,在不同的“描述”中。将它们结合在一起,是解决这个问题的唯一方法吗?
  • 您是否使用--parallel-p 标志运行测试?检查你的 npm 任务是否有 mocha 命令,没有那个 mocha 应该按照你编写它的顺序运行你的测试..
  • 我是这样做的:xhttp.open("POST", "http://localhost:3000/myPostApi", false); // false === synchronous/blocking

标签: node.js express mocha.js couchbase chai


【解决方案1】:

我认为您需要在“POST”测试中全力以赴。

我用这个方法来测试post API:

//Pseudocode
request(app)
  .post()
  .expect().then({
    //do assertions
    request(app)
      .get()
      .expect().then({
        //do assertions
      })
  })

我知道您必须将每个测试分成其套件,但这不是“GET 测试”进入“POST 套件”。这是一个 POST 测试,您使用 GET API 来断言数据已被插入。完全是“POST 测试”。

所以,代码将是这样的(我喜欢使用 done()):

it('should post', (done) => {
    request(app)
        .post('/endpoint')
        .expect(200).then(postResponse => {
            //assertion
            request(app)
                .get('/endpoint')
                .expect(200).then(getResponse => {
                    //assertion
                    done()
                }).catch(e => {
                    done(e)
                })
        }).catch(e => {
            done(e)
        })
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多