简体   繁体   中英

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

I've set up Cucumber-JS and Grunt-JS within my solution.

My folder structure looks like this:

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

I've added a Cucumber-JS task in gruntfile.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']);

And I've written out my feature file:

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

And my step definition file:

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

And my world.js file:

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

exports.World = World;

But when I run grunt at the command-line, while it seems to see my features, it never seems to run any of the steps.

All I get is this:

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 doesn't seem to pay any attention to what I put inside the tests.

Even if I put some obvious logical bug in, eg:

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

It just ignores it and prints the above message.

The only way I can seem to get any error out of Cucumber is to put an outright syntax error in the step file. Eg remove a closing bracket. Then I get something like this:

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.

What am I missing here?

As I said in my comments, everything is working as expected. Calling callback.pending() tells Cucumber your step definition is not ready yet and the rest of the scenario should be ignored for now.

Change that to callback() to tell Cucumber to move to the next step in the scenario. If you want to notify Cucumber of a failure, pass an error to that callback or throw an exception (I don't recommend that though):

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

HTH.

你有尝试过吗?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM