【问题标题】:Asynchronous Topic Scope Vows.JS异步主题范围誓言.JS
【发布时间】:2013-02-06 17:42:39
【问题描述】:

我无法将父 topic 值传递给子 topic 值。代码是异步的,我认为这就是我遇到问题的地方。我希望 JSON 响应的一部分成为下面测试的主题。这是测试的相关部分。

{
  "A test":{
    topic: function() {
      request(conf.server + '/categories/' + id, this.callback)
    },
    'should respond with a 200': function(err, res, body) {
      res.statusCode.should.equal(200);
      console.log(JSON.parse(body).title);
    },
    'should have valid JSON in the body': function(err, res, body) {
      (function() {
        JSON.parse(body);
      }).should.not.
      throw();
    },
    'category collection': {
      topic: function(err, res, body) {
        console.log(res.statusCode);
        return JSON.parse(body).categories
      },
      'should have a length greater than 0': function(topic) {
        topic.length.should.be.above(0);
      }
    }
  }
}

console.log(res.statusCode) 产生 undefined 并尝试将 topic 记录为“长度应大于 0”产生 [SyntaxError: Unexpected token u]

我可以这样做吗?如果有,怎么做?

【问题讨论】:

    标签: javascript node.js vows


    【解决方案1】:

    我对此进行了快速测试,似乎当第一个参数即 err 为空时,它不会传递给子上下文。所有其他参数都被传递。 这是我的代码。:

    module.exports = ( function() {
    var vows = require('vows'), assert = require('assert'), suite;
    suite = vows.describe('Vows test');
    suite.addBatch({
        'Parent context ' : {
            topic : function() {
                this.callback(null, "first", "second");
            },
            'err should be null' : function(err, first, second) {
                assert.isNull(err);
                assert.isNotNull(first);
                assert.isNotNull(second);
            },
            'subcontext: ' : {
                topic : function(err, first, second) {
                    console.log('Err: ' + err + ', first: ' + first + ', second: ' + second);
                    this.callback(null, "firstChild");
                },
                'Error should be null' : function(err, firstChild) {
                    assert.isNull(err);
                    assert.isNotNull(firstChild);
                }
            }
        }
    });
    
    suite.run();
    }());
    

    结果是Err: first, first: second, second: undefined ✓ OK » 2 honored

    但是当我传递错误的东西时,甚至不会打印日志和子上下文错误。

    我不知道这个的确切原因。我会检查誓言代码,如果我发现任何东西就会回来。 希望这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      相关资源
      最近更新 更多