【发布时间】:2014-11-09 08:41:25
【问题描述】:
我正在使用 restify,对于 TDD,我正在使用 mocha 测试框架。当我使用 post 方法从 restify 客户端测试 restify 服务器时,它没有按预期工作。
我的服务器代码如下:
exports.post_user = function(req,res,next) {
var body = req.body;
if (!body.name || !body.email) {
throw Error(JSON.stringify(body));
}
// Checking duplicate user before save user information into database
User.findOne({ email: body.email }, function (err, user) {
if(err) { req.log.error(err); }
if (!err && user != null) {
res.send(user);
} else {
var user = new User({
oauthID: Math.random(),
name: body.name,
email: body.email,
created : Date.now()
});
// Profile not available then save information and return response
user.save(function(err) {
if (err) {
console.log(err);
req.log.error(err);
} else {
res.send(user);
};
});
};
});
};
我的测试代码如下
describe('core', function () {
describe('/admin/user', function () {
it('POST with data', function (done) {
var data = {
name: "aruljoth1i",
email: "aruljot1h1iparthiban@hotmail.com"
};
client.post('/admin/user', data, function (err, req, res, obj) {
assert.equal(res.statusCode, 200,err);
done();
});
});
});
});
当我将数据作为 {name:'aruljothi'} 传递时,它正在工作,但与上述 json 对象的情况一样,它不起作用。在服务器中 req.body 以 {} 的形式出现。
错误
2) Expecting 200 status code post /admin/user -> should return 200 status:
Uncaught
AssertionError: InternalServerError: {"message":"{}"}
提前谢谢你。
【问题讨论】: