如何根据其他Mocha测试编写Mocha测试?

问题描述 投票:0回答:1

我正在尝试在Express REST API上运行一系列测试,强调单词系列,因为它们需要在某种程度上是顺序的。让我解释:

按顺序进行测试:

  1. API可以访问?
  2. API正确连接和验证?
  3. 然后可以使用Auth令牌写入数据存储?
  4. 然后可以使用Auth令牌来检索刚写入数据存储的数据?
  5. 然后可以使用Auth令牌来更改写入的数据(测试它是否与另一个检索一起使用)?

请注意,如果它们按顺序发生,这些步骤实际上只有任何意义,但是Mocha似乎没有按顺序运行测试。我可以做一个运行步骤1的测试,然后运行步骤1-2的另一个,然后运行步骤1-3的另一个,依此类推,但这绝对不是DRY。我也可以尝试建立一个beforeafters链,但这似乎不符合惯例。

有没有一种正确的方法来运行Mocha测试来检查流程中的顺序步骤?

我试图摆脱这种只是在重复测试中添加额外步骤的模式......

describe('Api', () => {
    it('should be accessible', (done) => {
        // Try to connect:
        Api.connect((error, conn) => {
            done(error);
        });
    });

    it('should connect and authenticate properly', (done) => {
        // Try to connect:
        Api.connect((error, api) => {
            if (error) done(error);
            // Then try to authenticate:
            api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
                done(error);
            });
        });
    });

    it('should allow for data to be written to the data store', (done) => {
        // Try to connect:
        Api.connect((error, api) => {
            if (error) done(error);
            // Then try to authenticate:
            api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
                if (error) done(error);
                // Then try to write data:
                conn.write(generateTestData(), (error, res) => {
                    done(err);
                });
            });
        });
    });

    it('should allow for written data to be read from the data store', (done) => {
        // Try to connect:
        Api.connect((error, api) => {
            if (error) done(error);
            // Then try to authenticate:
            api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
                if (error) done(error);
                let testData = generateTestData();
                // Then try to write data:
                conn.write(testData, (error, res) => {
                    if (error) done(error);
                    // Then try to read data:
                    conn.readLast((error, res) => {
                        if (error) done(error);
                        assert.equal(testData, res);
                    });
                });
            });
        });
    });
});
node.js express testing mocha chai
1个回答
0
投票

Mocha测试是连续运行的,但是,如果出现故障,它会继续进行下一次测试,因此看起来它们并行运行。

来自https://mochajs.org/

Mocha测试以串行方式运行,允许灵活准确的报告,同时将未捕获的异常映射到正确的测试用例

据说你可以在第一次失败后让它停止,但是我无法让这个标志起作用:

-b, - 首次测试失败后保释

测试之间传递状态的示例:

describe('Api', () => {
  var api, conn;

  it('should be accessible', (done) => {
    // Try to connect:
    Api.connect((error, new_api) => {
      if (error) {done(error);} else {
        api = new_api
        done();
      }
    });
  });

  it('should authenticate properly', (done) => {
    // Then try to authenticate:
    api.authenticate(TEST_AUTH_CREDENTIALS, (error, new_conn) => {
      if (error) {done(error);} else {
        conn = new_conn;
        done();
      }
    });
  });

  it('should allow for data to be written to the data store', (done) => {
    // Then try to write data:
    conn.write(generateTestData(), (error, res) => {
      if (error) {done(error);} else {
        done();
      }
    });
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.