2015-04-01 55 views
0

我正在嘗試爲發送表單數據的nodejs寫一個摩卡測試,並檢查響應是否正常(200),並且該res.body有一些屬性,但是測試失敗我不知道原因。增加timout沒有幫助,當我使用Payload部分中的表單數據的AdvancedRESTclient Chrome擴展其完美的作品!該.type('form')應該是SuperAgent的語法使用Mocha測試表格數據

var should = require('should'), 
    assert = require('assert'), 
    request = require('supertest'), 
    superagent = require('superagent'); 

     describe('Data', function() { 

      it('should return status OK (200)', function(done) { 
       this.timeout(20000); 
       request.post('http://xxx:3000/xxx/xxx') 
        .type('form') 
        .send({startDate:"2015-03-08",endDate:"2015-03-24",timeLapse:"day"}) 
        .end(function(err, res) { 
         if (err) { 
          throw err; 
         } 
         assert.ok(res); 
         assert.ok(res.body); 
         assert.equal(res.status, 200); 
         res.body.should.have.property('trial'); 
         done(); 
      }); 
     }); 

和錯誤是:

TypeError: undefined is not a function 
     at Context.<anonymous> (C:\Users\user\WebstormProjects\StatsTest\test\getMostRecentData.js:112:17) 
     at Test.Runnable.run (C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:233:15) 
     at Runner.runTest (C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:387:10) 
     at C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:470:12 
     at next (C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:312:14) 
     at C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:322:7 
     at next (C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:257:23) 
     at Immediate._onImmediate (C:\Users\user\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:289:5) 
     at processImmediate [as _immediateCallback] (timers.js:358:17) 

回答

0

試試這個:

var should = require('should'), 
    assert = require('assert'), 
    request = require('supertest')('http://xxx:3000'), 
    superagent = require('superagent'); 

     describe('Data', function() { 

      it('should return status OK (200)', function(done) { 

       request.post('/xxx/xxx') 
        .type('form') 
        .send({startDate:"2015-03-08",endDate:"2015-03-24",timeLapse:"day"}) 
        .end(function(err, res) { 
         if (err) { 
          throw err; 
         } 
         assert.ok(res); 
         assert.ok(res.body); 
         assert.equal(res.status, 200); 
         res.body.should.have.property('trial'); 
         done(); 
      }); 
     }); 
+0

它的工作!謝謝!爲什麼'request = require('supertest')('http:// xxx:3000'),'line必須包含'('http:// xxx:3000')'? – Isaac 2015-04-02 06:39:44

+0

不客氣,你可以閱讀'supertest'的文檔以獲取更多信息。 – Edgar 2015-04-02 06:47:45