node.js - Test form data using Mocha for NodeJS -
i trying write mocha test nodejs sends form data , checks if response ok (200), , res.body has properties, the test fails , dont know reason. increasing timout didnt help, when use advancedrestclient chrome extension form data in payload section works perfectly! .type('form')
supposed superagent syntax
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(); }); });
and error is:
typeerror: undefined not function @ context.<anonymous> (c:\users\user\webstormprojects\statstest\test\getmostrecentdata.js:112:17) @ test.runnable.run (c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runnable.js:233:15) @ runner.runtest (c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runner.js:387:10) @ c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runner.js:470:12 @ next (c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runner.js:312:14) @ c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runner.js:322:7 @ next (c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runner.js:257:23) @ immediate._onimmediate (c:\users\user\appdata\roaming\npm\node_modules\mocha\lib\runner.js:289:5) @ processimmediate [as _immediatecallback] (timers.js:358:17)
try this:
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(); }); });
Comments
Post a Comment