黄瓜JS可以看到我的功能,但似乎没有运行这些步骤

Cucumber JS can see my feature, but doesn't seem to run the steps

本文关键字:运行 JS 功能 我的 黄瓜      更新时间:2023-09-26

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

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

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

我在gruntfile中添加了一个Cucumber-JS任务.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();
    });
};

还有我的世界.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.

黄瓜似乎没有注意我在测试中放了什么。

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

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.

我在这里错过了什么?

正如我在评论中所说,一切都按预期工作。调用callback.pending()告诉 Cucumber 您的步骤定义尚未准备就绪,其余方案暂时应忽略。

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

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

呵。

你试过这个吗?

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