1

在 cucumber.js 中,我定义了一个步骤,例如:

this.When(/^I call getCollections$/, function(callback) {

    this.repo.getCollections( function( err, results ) {

        this.results = results;
        callback( err );

    }.bind( this ) );

});

但是如果调用this.repo.getColectionsnever 回调我的函数,则callback永远不会执行,并且 cucumber.js 会立即以正常的退出代码退出。

如果从不调用回调,有没有办法让 cucumber.js 失败?

4

2 回答 2

1

这些方面的东西应该可以解决问题:

this.Given(/^blah$/, function (callback) {
  var failed = false;
  var timeout = setTimeout(function () {
    callback(new Error("Timed out"));
  }, 500);

  doSomethingThatMightNeverCallback(function (err) {
    if (!failed) {
      clearTimeout(timeout);
      callback(err);
    }
  });
});

您可以轻松地重新定义 Cucumber 的 Given/When/Then:

var cucumber = this;
var Given = When = Then = defineStep;
function defineStep (name, handler) {
  cucumber.defineStep(name, function () {
    var world = this;
    var args = Array.prototype.splice.call(arguments, 0);
    var callback = args.pop();

    var failed = false;
    var timeout = setTimeout(function () {
      callback(new Error("Timed out"));
    }, 500);

    args.push(function (err) {
      if (!failed) {
        clearTimeout(timeout);
        callback(err);
      }
    });
    handler.apply(world, args);
  });
}

Given()然后使用,When()Then()不是等定义您的步骤定义this.Given()

Given(/^foo$/, function (callback) {
  setTimeout(callback, 400); // this will pass
});

Given(/^bar$/, function (callback) {
  setTimeout(callback, 500); // this will fail
});

When(/^baz$/, function (callback) {
  doYourStuff(callback);
});

正如预期的那样,我在步骤 2 中遇到了以下情况:

Feature: Baz

  Scenario:
    Given foo
    And bar

输出:

cucumber.js test.feature 
.F

(::) failed steps (::)

Error: Timed out
    at null._onTimeout (/Users/jbpros/tmp/abc/code.js:13:18)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Failing scenarios:
/Users/jbpros/tmp/abc/test.feature:3 # Scenario: 

1 scenario (1 failed)
2 steps (1 failed, 1 passed)

高温下,

  • 朱利安。
于 2014-01-19T10:11:31.900 回答
0

这是我最终做的事情:

在模块 utils.js 中,我公开了一个函数

function specifyTimeout( scenario, timeout ) {

    scenario.Before( function( callback ) {

        scenario.timeout = setTimeout( function() { throw new Error("Timed out"); }, 500 );
        callback();

    } );

    scenario.After( function( callback ) {

        clearTimeout( scenario.timeout );
        callback();

    } );

}

然后在我的步骤定义的顶部,我只是这样做:

module.exports = function() {

    utils.specifyTimeout( this, 500 );
    . . .
于 2014-01-18T18:44:37.130 回答