繁体   English   中英

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

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

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

这有效:

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

当我通过以下方式运行它时:

node tests/learning-vows.js

我得到:

·
·
✓ OK » 2 honored (0.207s)

这不起作用:

我有一个文件./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()

当我通过以下方式执行此操作时:

node tests/smoke.js

我得到:

·
✗ Errored » 1 honored ∙ 1 errored

请注意,在第二个示例中,没有子主题,我得到:

·
✓ OK » 1 honored (0.100s)

Vows使用节点的约定进行回调(请参阅: http : //nodemanual.org/latest/nodejs_dev_guide/working_with_callbacks.html ),它假定回调的第一个参数是一个错误对象。

因此,当您将data作为第一个参数发送时,您的誓言誓言client.register发生了错误。 它防止誓言评估子主题。 子主题被标记为错误,但是断言成功,当前主题被标记为已接受。

从输出中猜测出来确实不是一件容易的事。 此外,誓言行为不一致,请在第一个测试中尝试将true替换为0 ,然后将'0'用作回调参数,您将看到另外两个结果。

这是一个工作示例:

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