简体   繁体   中英

What is wrong with my usage of vows.js sub-topics?

For some reason, I can't seem to get vows.js sub-topics working in my real test-suite, but they work fine in a example file... can you spot my problem?

This works:

vows.describe('An Education in Vows').addBatch({
    'when I write an asynchronous topic': {
        topic: function() {
            var that = this;
            setTimeout(function() {
                that.callback(true);
            }, 100);
        },
        'it passes': function (topic) {
            assert.equal(topic, true);
        },
        'and it has an asynchronous sub-topic': {
            topic: function() {
                var that = this;
                setTimeout(function() {
                    that.callback(true);
                }, 100);
            },
            'it also passes': function (topic) {
                assert.equal(topic, true);
            }
        }
    }
}).run();

When I run this via:

node tests/learning-vows.js

I get:

·
·
✓ OK » 2 honored (0.207s)

This Doesn't work:

I have a file ./tests/smoke.js

vows.describe('Registration & Authentication').addBatch({
    'when a user registers with a valid username and password': {
        topic: function () {
            client.register({
                username: validusername,
                password: validpassword
            }, this.callback);
        },
        'we return status 200 OK': function (data, response) {
            assert.equal(200, response.statusCode);
        },
        'simple sub-topic': {
            topic: true,
            'should work': function(topic) {
                assert.equal(true, topic);
            }
        },
    }
}).run()

When I execute this via:

node tests/smoke.js

I get:

·
✗ Errored » 1 honored ∙ 1 errored

Note that in the second example, without the sub topic I get:

·
✓ OK » 1 honored (0.100s)

Vows is using node's convention for callbacks (see : http://nodemanual.org/latest/nodejs_dev_guide/working_with_callbacks.html ), it assumes that first parameter on callback is an error object.

So when you send data as first parameter your telling vows that an error happened in client.register . It prevents vows from evaluating sub-topic. Sub-topic is marked errored but assertion succeeded and current topic is marked as honored.

It is really not trivial to guess that from output. Moreover vows behavior is not consistent, try replacing true to 0 and then '0' as callback parameter in your first test and you will see two other results.

Here is a working exemple :

var vows = require('vows'), assert = require('assert');

var client = {
  register: function(obj,callback){
    callback(null, obj, {statusCode:200});
  }
};
vows.describe('Registration & Authentication').addBatch({
    'when a user registers with a valid username and password': {
        topic: function () {
            client.register({
                username: 'validusername',
                password: 'validpassword'
            }, this.callback);
        },
        'we return status 200 OK': function (err, data, response) {
            assert.equal(response.statusCode, 200);
        },
        'simple sub-topic': {
            topic: true,
            'should work': function(topic) {
                assert.equal(true, topic);
            }
        }
    }
}).export(module)

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