[英]Asynchronous Topic Scope Vows.JS
我在将父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)
产生未定义的内容,并尝试以“应该大于0的长度”记录该topic
产生[SyntaxError: Unexpected token u]
。
我可以这样做吗? 如果是这样,怎么办?
我对此进行了快速测试,似乎当第一个参数(即err)为null时,它不会传递给子上下文。 传递所有其他参数。 这是我的代码:
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
。
但是,当我传递错误消息时,甚至不会打印日志和子上下文错误。
我不知道确切的原因,我将检查誓言代码并在发现任何内容后返回。 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.