Cucumber JS可以看到我的功能,但似乎没有运行步骤

时间:2013-11-12 01:47:20

标签: javascript cucumber gruntjs

我在我的解决方案中设置了Cucumber-JS和Grunt-JS。

我的文件夹结构如下所示:

+ Project
  + features
    - Search.feature
    + step_definitions
      - Search_steps.js
    + support
      - world.js
  - package.json
  - gruntfile.js

我在gruntfile.js中添加了一个Cucumber-JS任务:

// Project configuration.
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    cucumberjs: {
        src: 'features',
        options: {
            steps: 'features/step_definitions',
            format: 'pretty'
        }
    }
});

grunt.loadNpmTasks('grunt-cucumber');

grunt.registerTask('default', ['cucumberjs']);

我已写出我的专题文件:

Feature: Search
    As a user of the website
    I want to search
    So that I can view items

    Scenario: Searching for items
        Given I am on the website
        When I go to the homepage
        Then I should see a location search box

我的步骤定义文件:

var SearchSteps = module.exports = function () {
    this.World = require('../support/world').World;

    this.Given('I am on the website', function(callback) {
        callback.pending();
    });

    this.When('I go to the homepage', function (callback) {
        callback.pending();
    });

    this.Then('I should see a location search box', function (callback) {
        callback.pending();
    });
};

我的world.js文件:

var World = function (callback) {
    callback(this);
};

exports.World = World;

但是当我在命令行中运行grunt时,虽然它似乎看到了我的功能,但似乎从未执行任何步骤。

我得到的就是这个:

Running "cucumberjs:src" (cucumberjs) task
Feature: Search

  Scenario: Searching for items
    Given I am on the website
    When I go to the homepage
    Then I should see a location search box


1 scenario (1 pending)
3 steps (1 pending, 2 skipped)

Done, without errors.

Cucumber似乎没有注意我在测试中的内容。

即使我提出了一些明显的逻辑错误,例如:

this.Given('I am on the website', function(callback) {
    var x = 0 / 0;
    callback.pending();
});

它只是忽略它并打印上述信息。

我似乎从Cucumber中获得任何错误的唯一方法是在步骤文件中放置一个彻头彻尾的语法错误。例如。删除一个右括号。然后我得到这样的东西:

Running "cucumberjs:src" (cucumberjs) task

C:\dev\Project\features\step_definitions\Search_steps.js:14
                };
                 ^
Warning: Unexpected token ; Use --force to continue.

Aborted due to warnings.

我在这里缺少什么?

2 个答案:

答案 0 :(得分:5)

正如我在评论中所说,一切都按预期工作。调用callback.pending()告诉Cucumber你的步骤定义还没有准备好,现在应该忽略其余的场景。

将其更改为callback(),告诉Cucumber进入方案的下一步。如果您想通知Cucumber失败,请将错误传递给该回调或抛出异常(我不建议这样做):

callback(new Error('This is a failure'));

HTH。

答案 1 :(得分:0)

你试过这个吗?

this.World = require("../support/world.js").World;