Deleting our test user

First, add a new entry to the list of features in the Cucumber specification:

...
And the payload of the response should be a string
And the payload object should be added to the database, grouped under the "user" type
And the newly-created user should be deleted

Next, define the corresponding step definition for this step. But first, we are going to modify the step definition that indexed the document, and change it to persist the document type in the context:

Then(/^the payload object should be added to the database, grouped under the "([a-zA-Z]+)" type$/, function (type, callback) {
this.type = type;
client.get({
index: 'hobnob'
type: type,
id: this.responsePayload
})
...
});

Then, add a new step definition that uses client.delete to delete a document by ID:

Then('the newly-created user should be deleted', function () {
client.delete({
index: 'hobnob',
type: this.type,
id: this.responsePayload,
});
});

The result of the delete method looks something like this:

{ _index: 'hobnob',
_type: 'user',
_id: 'N2hWu2ABiAD9b15yOZTt',
_version: 2,
result: 'deleted',
_shards: { total: 2, successful: 1, failed: 0 },
_seq_no: 4,
_primary_term: 2 }

A successful operation will have its result property set to 'deleted'; therefore, we can use it to assert whether the step was successful or not. Update the step definition to the following:

Then('the newly-created user should be deleted', function (callback) {
client.delete({
index: 'hobnob',
type: this.type,
id: this.responsePayload,
}).then(function (res) {
assert.equal(res.result, 'deleted');
callback();
}).catch(callback);
});

Run the tests and make sure they pass. We've now implemented our happy path/success scenario, so it's a good time to commit our changes:

$ git add -A && git commit -m "Implement Create User success scenario"