【问题标题】:Mocha/Should.js using asynchronous function [duplicate]Mocha/Should.js 使用异步函数 [重复]
【发布时间】:2016-05-24 14:14:50
【问题描述】:

我是 JavaScript 测试框架的新手。我想做一些优化,但我遇到了一些问题。该项目正在使用should.js

这是我原始测试用例的简化版本:

describe('Simple', function() {
  describe('Test', function() {
    it('should do something', function(done) {
      somePromise.then(function(data) {
        data.should.above(100);
        done();
      });
    }

    it('should do something else but alike', function(done) {
      somePromise.then(function(data) {
        data.should.above(100);
        done();
      });
    }

  }
});

我正在尝试这样做:

var testFunc = function(data) {
  it('should do something', function(done) {
      data.should.above(100);
      done();
  });
}

describe('Simple', function() {
  describe('Test', function() {
      somePromise.then(function(data) {
        testFunc(data);
      });

      somePromise.then(function(data) {
        testFunc(data);
      });
  }
});

承诺是异步的,也许这就是我的“优化”不起作用的原因?我在文档中找不到 describe 函数的“完成”回调。

提前致谢!任何帮助将不胜感激!

【问题讨论】:

    标签: javascript node.js testing mocha.js should.js


    【解决方案1】:

    您的示例不起作用since Mocha has finished registering test cases when your promise resolves

    用不同的断言测试同一个 Promise

    要使用多个断言测试单个 Promise,您只需在测试开始时创建 Promise,然后在 it 块中使用它,如下所示:

    describe('A module', function() {
        var promise;
    
        before(function () {
            promise = createPromise();
        });
    
        it('should do something', function() {
            return promise.then(function (value) {
                value.should.be.above(100);
            });
        });
    
        it('should do something else', function() {
            return promise.then(function (value) {
                value.should.be.below(200);
            });
        });
    });
    

    请注意,如果从 API 调用返回 Promise,则该调用只会进行一次。 The result is simply cached in the promise for the two test cases.

    这也利用了 you can return promises from test cases 的事实,而不是使用 done 回调。如果 promise 被拒绝或 then() 调用中的任何断言失败,则测试用例将失败。

    用相同的断言测试不同的 Promise

    假设您想使用相同的断言测试不同的 Promise,您可以将一个函数传递给 testFunc 以创建要测试的 Promise。

    var testFunc = function(promiseFactory) {
      it('should do something', function(done) {
          promiseFactory().then(function(data) {
              data.should.above(100);
              done();
          });
      });
    }
    
    describe('Simple', function() {
      describe('Test', function() {
          testFunc(function () { return createSomePromise(); });
    
          testFunc(function () { return createSomeOtherPromise(); });
      });
    });
    

    这是因为 Mocha 的 it 函数立即在 describe 块内运行。然后在实际运行测试用例时使用promiseFactory 回调创建承诺。

    您也可以return promises from test cases 更改testFunc 以将断言作为承诺返回,如下所示:

    var testFunc = function(promiseFactory) {
      it('should do something', function() {
          return promiseFactory().should.eventually.be.above(100);
      });
    }
    

    【讨论】:

    • 谢谢,它有效。但是我可以使用不同的断言来测试一个 Promise 吗?
    • 我试图将it 函数包装在我的promise 函数中,这样我就可以在我的promise 函数中请求异步api 一次但做不同的测试
    • 当然可以。 Promises 使这变得非常容易。您所做的是在测试用例之外创建承诺并将其分配给变量。然后,您可以像往常一样使用它块中的承诺。我已经用一个例子更新了我的答案。
    • 谢谢!我应该更深入地了解 Promise。再次感谢你:)
    猜你喜欢
    • 2012-09-14
    • 2019-04-30
    • 1970-01-01
    • 2012-08-22
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多