简体   繁体   中英

Asynchronous Topic Scope Vows.JS

I'm having trouble passing parent topic values to children topic values. The code is asynchronous and I think that is where I'm having the problem. I want a part of the JSON response to be the topic of the tests underneath. Here is the relevant parts of the tests.

{
  "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) yields undefined and trying to log the topic in "should have a length greater than 0" yields [SyntaxError: Unexpected token u] .

Can I do this? If so, how?

I did a quick test on this, it seems that when first parameter ie err is null, it is not passed to sub-context. All other parameters are passed. Here is my code.:

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

Result was Err: first, first: second, second: undefined ✓ OK » 2 honored .

But when I pass something in error, log is not even printed and sub-context errors.

I don't know exact reasons of this.I will check vows code and get back if I find anything. Hope this is somewhat useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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