【问题标题】:Supertest Error: expected '[]' response body, got '[""]'超测错误:预期 '[]' 响应正文,得到 '[""]'
【发布时间】:2017-10-16 18:13:12
【问题描述】:

我不确定为什么我不断收到此错误。每当我运行 npm test 时,我都会收到这个完整的错误:

1) Listing subscriptions on /subscriptions
   Returns initial subscriptions:
 Error: expected '[]' response body, got '[""]'
  at error (node_modules\supertest\lib\test.js:299:13)
  at Test._assertBody (node_modules\supertest\lib\test.js:216:14)
  at Test._assertFunction (node_modules\supertest\lib\test.js:281:11)
  at Test.assert (node_modules\supertest\lib\test.js:171:18)
  at Server.assert (node_modules\supertest\lib\test.js:131:12)
  at emitCloseNT (net.js:1543:8)
  at _combinedTickCallback (internal/process/next_tick.js:71:11)
  at process._tickCallback (internal/process/next_tick.js:98:9)

在我的 app.js 文件中:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencode = bodyParser.urlencoded({ extended: false });

app.use(express.static('public'));

var redis = require('redis');
if (process.env.REDISTOGO_URL) {
    var rtg = require('url').parse(process.env.REDISTOGO_URL);
    var client = redis.createClient(rtg.port, rtg.hostname);
    client.auth(rtg.auth.split(":")[1]);
} else {
    var client = redis.createClient();
    client.select((process.env.NODE_ENV || 'development').length);
}

app.get('/subscriptions', function (req, res) {
    client.hkeys('subscriptions', function (err, names) {
        if (err) throw err;
        res.json(names);
    });
});


app.post('/subscriptions', urlencode, function (req, res) {
var newSubscription = req.body;
if(!newSubscription.name || !newSubscription.date) {
    res.sendStatus(400);
    return false;
}
client.hset('subscriptions', newSubscription.name,  newSubscription.date, 
function (err) {
    if (err) throw err;
    res.status(201).json(newSubscription.name);
});
});

test.js 文件:

var request = require('supertest');
var app = require('./app');

var redis = require('redis');
var client = redis.createClient();
client.select('test'.length);
client.flushdb();

describe('Requests to the root path', function () {

    it('Returns a 200 status code', function (done) {

        request(app)
            .get('/')
            .expect(200, done);
    });
    it('Returns a HTML format', function(done) {
        
        request(app)
            .get('/')
            .expect('Content-Type', /html/, done);
    });
    it('Returns an index file with Subscriptions', function(done) {

        request(app)
            .get('/')
            .expect(/subscriptions/i, done);
    });
});

describe('Listing subscriptions on /subscriptions', function () {

    it('Returns 200 status code', function (done) {

        request(app)
            .get('/subscriptions')
            .expect(200, done);

    });
    it('Returns JSON format', function (done) {

        request(app)
            .get('/subscriptions')
            .expect('Content-Type', /json/, done);
    });

    it('Returns initial subscriptions', function (done) {

        request(app)
            .get('/subscriptions')
            .expect(JSON.stringify([]), done);
    });
});

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: javascript node.js unit-testing redis supertest


    【解决方案1】:

    最终使用

    client.flushall();

    代替

    client.flushdb();

    它清除了所有现有数据(尽管我不确定它是如何进入那里的)。后来我把它改回来,它工作正常。

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 2014-05-14
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      相关资源
      最近更新 更多