Node.js - Restify POST Mocha测试失败

时间:2016-11-09 16:01:32

标签: node.js post mocha restify

我收到错误“来自/发布的回复无效”

at main.js:23:27       在parseResponse(node_modules / restify / lib / clients / json_client.js:91:9)       在IncomingMessage.done(node_modules / restify / lib / clients / string_client.js:165:13)       在endReadableNT(_stream_readable.js:913:12)

这是我的代码:

function get_settings(req, res, next) {
var text = "";
req.setEncoding("utf8");
res.send(200, {ok: 'ok'});
}
var server = restify.createServer({
name: 'test',
version: '0.0.1'});

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.post('/post', get_settings );

server.listen(5177,function() {
console.log('%s listening at %s', server.name, server.url);
});

摩卡测试:

var restify = require('restify');
var assert = require('assert');

// init the test client
var client = restify.createJsonClient({
url: 'http://141.37.168.79:5177',
version: '*'
});

describe('service: post endpoint', function() {

// Test #1
describe('200 response check', function() {
it('should get a 200 response', function(done) {
    client.post('/post', { hello: 'world' }, function(err, req, res, data) {                
        if (err) {
            throw new Error(err);
        }
        else {

            if (data.code) != 200) {
                throw new Error('invalid response from /post');
            }
            done();
        }
    });
});
});

});

1 个答案:

答案 0 :(得分:0)

乍一看

if (data.code) != 200)

看起来不太好。那里你有一个额外的括号。

除此之外,我认为您访问状态代码的方式是使用res.statusCode