0

我正在尝试为 nodejs 编写一个 mocha 测试,它发送表单数据并检查响应是否正常(200),并且 res.body 具有一些属性,但是测试失败并且我不知道原因。增加 timout 没有帮助,当我使用 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)
4

1 回答 1

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();
            });
        });
于 2015-04-01T13:11:43.563 回答