2012-02-28 111 views
1

當我在下面運行此代碼時,用戶已登錄並且瀏覽器被重定向到一條新路線home。我如何構建下面的測試來驗證/home正被重定向到。Superagent和Mocha測試節點

該應用程序工作正常,我只是試圖讓我的測試用例順序。

感謝

it('should login user', 
    function(done) { 
     request 
     .post(url.parse('http://localhost:3000/login')) 
     .send({ 
      userName: "[email protected]", password: "xyzpassword" 
     }) 
     .end(function(res) { 
      res.statusCode.should.equal(302); 
      done(); 
     }) 
    }); 

更多相關信息......代碼的作品,但與STACKDUMP失敗,所以我無法驗證測試成功與否

1) authentication_tests should login user: 
    TypeError: first argument must be a string, Array, or Buffer 
     at ClientRequest.write (http.js:601:11) 
     at ClientRequest.end (http.js:681:16) 
     at Request.end (/Users/aaronksaunders/dev/node_stuff/sqchic/node_modules/superagent/lib/node/index.js:602:7) 
     at Request.redirect (/Users/aaronksaunders/dev/node_stuff/sqchic/node_modules/superagent/lib/node/index.js:459:8) 
     at ClientRequest.<anonymous> (/Users/aaronksaunders/dev/node_stuff/sqchic/node_modules/superagent/lib/node/index.js:569:49) 
     at ClientRequest.emit (events.js:64:17) 
     at HTTPParser.<anonymous> (http.js:1349:9) 
     at HTTPParser.onHeadersComplete (http.js:108:31) 
     at Socket.ondata (http.js:1226:22) 
     at Socket._onReadable (net.js:683:27) 
     at IOWatcher.onReadable (net.js:177:10) 

回答

0

superagent docs

響應標題字段

res.header包含解析的頭字段對象,與節點一樣降低字段名。例如res.header ['content-length']。

所以,你可以沿着線檢查:

(/home$/).test res.header['location'] 

對於您的重定向測試案例。

+0

問題是代碼可以正常工作,但是測試工具在堆棧轉儲時失敗,請參閱上面的編輯 – 2012-02-28 14:32:25

+0

這可能是因爲您正在將url.parse返回的對象傳遞給request.post。只需將URL作爲字符串直接傳遞給request.post即可。 – 2012-02-28 14:49:25

相關問題