使用Mocha for NodeJS测试表单数据

时间:2015-04-01 13:01:15

标签: node.js testing mocha superagent

我正在尝试为发送表单数据的nodejs编写一个mocha测试,并检查响应是否正常(200),并且res.body有一些属性,但测试失败和我不知道原因。增加时间没有帮助,当我使用AdvancedRESTclient chrome扩展与Payload部分中的表单数据时,它的工作完美! .type('form')应该是超级语法

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)

1 个答案:

答案 0 :(得分: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();
            });
        });