2015-04-01 78 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

相关问题