首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步主题作用域Vows.JS

异步主题作用域Vows.JS
EN

Stack Overflow用户
提问于 2013-02-07 01:42:40
回答 1查看 143关注 0票数 1

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

代码语言:javascript
复制
{
  "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)会产生未定义的结果,尝试将topic记录在“应该具有大于0的长度”中会产生[SyntaxError: Unexpected token u]

我能这么做吗?如果是这样的话,是怎么做的?

EN

回答 1

Stack Overflow用户

发布于 2013-08-17 18:41:44

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

代码语言:javascript
复制
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

但是当我传递一些错误的东西时,日志甚至没有打印出来,而且还有子上下文错误。

我不知道这的确切原因。我会检查誓言代码,如果我发现了什么就回来。希望这篇文章能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14735433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档