簡體   English   中英

異步主題范圍Vows.JS

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM