2012-01-09 37 views
1

我對node.js測試完全陌生,也許你可以幫我解決: 我想對使用誓言和tobi的express webapp做一些或多或少的簡單測試(例如測試如果登錄路由的工作原理)使用Vows和Tobi進行Webapp測試

var vows = require('vows'); 
var assert = require('assert'); 
var tobi = require('tobi'); 

var browser = tobi.createBrowser(8080, 'localhost'); 

vows.describe('mytest').addBatch({ 

    'GET /': { 
     topic: function() { 

      browser.get("/", this.callback); 

     }, 
     'has the right title': function(res, $) { 

      $('title').should.equal('MyTitle'); 

     } 
    } 


}).export(module); 

,我得到這個:

♢ mytest 

GET/
    ✗ has the right title 
     » expected { '0': 
    { _ownerDocument: 

    [....lots of stuff, won't paste it all.....] 

    Entity: [Function: Entity], 
    EntityReference: [Function: EntityReference] } }, 
    selector: ' title' } to equal 'MyTitle' // should.js:295 

✗ Broken » 1 broken (0.126s) 

我不能認識到什麼是從這個輸出錯誤的,但我猜它的人做回調。我也是node.js中異步編程風格的新手。

回答

1

誓言期望回調的第一個參數是一個錯誤。如果它不是null或undefined,它認爲是錯誤的。您必須將回調包裝到匿名函數中,該函數使用null作爲其第一個參數進行調用。

vows.describe('mytest').addBatch({ 

    'GET /': { 
     topic: function() { 
      var cb = this.callback; 
      browser.get("/", function() { 
       var args = Array.prototype.slice.call(arguments); 
       cb.apply(null, [null].concat(args)); 
      }); 

     }, 
     'has the right title': function(err, res, $) { 

      $('title').should.equal('MyTitle'); 

     } 
    } 


}).export(module); 
+0

這個代碼拋出了同樣的錯誤:( – toxinlabs 2012-01-10 18:36:35

+1

誓言是一種奇怪的,如果你在你的回調一個參數,第一個參數的東西調用它,它會認爲這是一個錯誤,告訴你有一個錯誤如果你有多個它認爲你想自己處理錯誤並且不會拋出錯誤嘗試在你的原始代碼中添加一個額外的錯誤參數 – fent 2012-01-10 19:50:28

+0

這並沒有很好的記錄在誓言中, – fent 2012-01-10 19:51:07