【问题标题】:What is wrong with my usage of vows.js sub-topics?我对 vows.js 子主题的使用有什么问题?
【发布时间】:2013-03-05 14:56:43
【问题描述】:

出于某种原因,我似乎无法让 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)

【问题讨论】:

  • 你试过console.log你最后一个assert.equal正在测试什么主题吗?
  • 如果你用topic: function() { return true; }代替topic: true,会怎样

标签: javascript node.js bdd vows


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多