Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用异步函数的Mocha/Should.js_Javascript_Node.js_Testing_Mocha.js_Should.js - Fatal编程技术网

Javascript 使用异步函数的Mocha/Should.js

Javascript 使用异步函数的Mocha/Should.js,javascript,node.js,testing,mocha.js,should.js,Javascript,Node.js,Testing,Mocha.js,Should.js,我是JavaScript测试框架的新手。我想做一点优化,但我遇到了一些问题。该项目正在使用 以下是我的原始测试用例的简化版本: describe('Simple', function() { describe('Test', function() { it('should do something', function(done) { somePromise.then(function(data) { data.should.above(100);

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

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

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);
      });
  }
});
承诺是异步的,也许这就是我的“优化”不起作用的原因?我在文档中没有发现
descripe
函数的“done”回调


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

您的示例不起作用

用不同的断言测试相同的承诺 要使用多个断言测试单个承诺,只需在测试开始时创建承诺,然后在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调用返回的,那么调用将只进行一次

这还利用了这样一个事实,即不使用done回调。如果承诺被拒绝,或者如果then()调用中的任何断言失败,那么测试用例将失败

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

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
功能在descripe块内立即运行。当测试用例实际运行时,使用
promiseFactory
回调创建承诺

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

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

谢谢,它起作用了。但是我可以使用不同的断言来测试一个承诺吗?我正在尝试将
it
函数包装到我的承诺函数中,这样我就可以在我的承诺函数中请求异步api一次,但当然可以执行不同的测试。承诺让这变得很容易。您所做的是在测试用例之外创建承诺,并将其分配给变量。然后,您可以像往常一样使用它块中的承诺。我已经用一个例子更新了我的答案。谢谢!我应该更深入地寻找承诺。再次感谢:)