【问题标题】:Mocha: Multiple "it" calls in single server requestMocha:单个服务器请求中的多个“it”调用
【发布时间】:2016-07-06 17:37:17
【问题描述】:

我正在尝试使用 Mocha 测试 40 多个 API 端点。作为单个服务器调用的一部分,我想执行一些子测试。

比如我想测试it('returns valid JSON...it('returns a valid status code...

configs.forEach(function(config) {

    describe(config.endpoint, () => {

        it('...', function(done) {
            server
                .post(config.endpoint)
                .send({})
                .expect('Content-type', /json/)
                .expect(200)
                .end(function(err, res) {

                    //it('has a proper status code', () => {
                    expect(res.status).toEqual(200);
                    //})

                    //it('does not have an error object', () => {
                    expect(res.body.hasOwnProperty('error')).toEqual(false);
                    //})

                    done();
                })
        })

    })

})

问题是我不能嵌套it 语句,但我依靠回调,通过done() 来指示何时收到响应,所以我必须将调用包装在it 语句中。 ..

因为其中一些请求需要半秒才能解决,并且其中有 40 多个,我不想为这些请求创建单独的测试。创建单独的测试也会复制 config.endpoint,我想看看每个端点的测试是否都在一个地方通过。

如何为单个服务器调用创建多个测试?

【问题讨论】:

  • 模拟回调,它认为它会变得容易吗?
  • 模拟来自it(... 的回调?不知道你的意思。

标签: javascript testing mocha.js


【解决方案1】:

以下是我使用 mocha、chai 和 supertest(API 请求)完成此操作的方法:

import { expect } from "chai"

const supertest = require("supertest");
const BASE_URL = process.env.API_BASE_URL || "https://my.api.com/";
let api = supertest(BASE_URL);

describe("Here is a set of tests that wait for an API response before running.", function() {

  //Define error & response in the 'describe' scope. 
  let error, response;

  //Async stuff happens in the before statement.
  before(function(done) {
    api.get("/dishes").end(function(err, resp) {
      error = err, response = resp;
      done();
    });
  });

  it("should return a success message", function() {
    console.log("I have access to the response & error objects here!", response, error);
    expect(response.statusCode).to.equal(200);
  });

  it("should return an array of foos", function() {
    expect(response.body.data.foo).to.be.an("array");
  });
});

【讨论】:

    【解决方案2】:
        configs.forEach(function(config) {
    
            describe(config.endpoint, () => {
    
                var response;    
                it('...', function(done) {
                    server
                        .post(config.endpoint)
                        .send({})
                        .expect('Content-type', /json/)
                        .expect(200)
                        .end(function(err, res) {
                              response=res;
                              done();  
                          })
                 });
    
                        it('has a proper status code', () => {
                              expect(response.status).toEqual(200);
                        })
    
                        it('does not have an error object', () => {
                                expect(response.body.hasOwnProperty('error')).toEqual(false);
                        })
    
             })
    })
    

    这个呢? 我不确定测试用例的嵌套,但它对你有用。

    【讨论】:

    • 你不应该依赖测试顺序。在这种情况下,您依赖于第一个 it 在以下两个之前执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多