testing蓝鸟承诺与Nodejs誓言(BDD)

我遇到了一些问题,比如如何正确构build我的Promise返回API的testing

topic:function() { return myfunc() { /* returns a Bluebird Promise */ } }, 'this should keep its promise':function(topic) { var myfunc = topic; myfunc() .then(function(result) { assert(false); }) .catch(function(error) { assert(false); }) .done(); } 

我的誓言永远不会失败。 这是我第一次尝试使用誓言来testing承诺。 希望有人熟悉这一点会伸出援助之手。

提前,谢谢。

恩里克

由于与Mocha这样的库不同,Vows还没有支持testing承诺,所以我们使用它的常规asynchronoustesting格式来callback:

 topic:function() { return myfunc() { /* returns a Bluebird Promise */ } }, 'this should keep its promise':function(topic) { var myfunc = topic; myfunc() // call nodeify to turn a promise to a nodeback, we can chain here .nodeify(this.callback); // note the this.callback here } 

下面是摩卡的外观:

 describe("Promises", function(){ it("topics", function(){ return myfunc(); // chain here, a rejected promise fails the test. }); }) 

下面的例子是使用一个when js风格承诺与誓言。 你应该能够适应你正在使用的诺言的任何味道。 关键是:

1)确保在您的承诺解决时调用this.callback。 我在下面的例子中将“this”赋值给一个variables,以确保在promiseparsing时它是正确可用的。

2)用一个err对象和你的结果调用这个callback函数(见下面这个是如何完成这个variables的) 。 如果你只是用你的结果来调用它,誓言就会把它解释为一个错误。

  vows.describe('myTests') .addBatch({ 'myTopic': { topic: function() { var vow = this; simpleWhenPromise() .then(function (result) { vow.callback(null, result); }) .catch(function (err) { vow.callback(result, null); }); }, 'myTest 1': function(err, result) { // Test your result } }, })