2

我在如何使用 Vows 为我的 Promise-returning API 正确构建测试时遇到问题,例如

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();
}

我的誓言永不落空。这是我第一次尝试使用 vows 来测试 Promise。希望熟悉这方面的人伸出援助之手。

提前谢谢你。

恩里克

4

2 回答 2

2

由于与 Mocha 之类的库不同 - Vows 尚不支持测试承诺,因此我们使用其常规的异步测试格式,该格式需要回调:

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.
   });
})
于 2014-08-23T21:14:30.070 回答
0

以下示例使用带有誓言的 when js 样式承诺。您应该能够使其适应您正在使用的任何形式的承诺。关键点是:

1) 确保在您的承诺解决时调用 this.callback。我在下面的示例中将“this”分配给一个变量,以确保它在 promise 解决时正确可用。

2)使用 err 对象和您的结果调用 this.callback (请参阅下面如何使用变量完成此操作) 。如果你只是用你的结果来调用它,vows 会将它解释为一个错误。

  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
    }
  },
})
于 2015-08-12T19:30:37.713 回答