简体   繁体   English

我对vows.js子主题的使用有什么问题?

[英]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? 出于某种原因,我似乎无法在真正的测试套件中使用vows.js子主题,但是在示例文件中它们可以正常工作……您能发现我的问题吗?

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 我有一个文件./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. Vows使用节点的约定进行回调(请参阅: http : //nodemanual.org/latest/nodejs_dev_guide/working_with_callbacks.html ),它假定回调的第一个参数是一个错误对象。

So when you send data as first parameter your telling vows that an error happened in client.register . 因此,当您将data作为第一个参数发送时,您的誓言誓言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. 此外,誓言行为不一致,请在第一个测试中尝试将true替换为0 ,然后将'0'用作回调参数,您将看到另外两个结果。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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