2013-02-06 3 views
1

topic 부모 값을 자식 topic 값으로 전달하는 데 문제가 있습니다. 코드는 비동기식이며 문제가있는 곳이라고 생각합니다. JSON 응답의 일부가 테스트의 주제가되기를 바란다. 다음은 테스트의 관련 부분입니다.비동기 주제 범위 Vows.JS

{ 
    "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)은 정의되지 않은 수율 [SyntaxError: Unexpected token u] "0보다 길이가 더 있어야한다"의 topic를 로그인을 시도 얻을 수 있습니다.

이 작업을 수행 할 수 있습니까? 그렇다면 어떻게?

답변

0

첫 번째 매개 변수 즉 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했다입니다.

하지만 오류가있는 부분을 전달하면 로그가 인쇄되지 않고 하위 컨텍스트 오류가 발생합니다.

나는 정확한 이유를 알지 못합니다. 나는 코드를 확인하고 아무것도 발견하면 돌아올 것입니다. 희망 사항은 다소 유용 할 것입니다.