我正在用 supertest 编写单元测试来测试我的服务器。但是我的一个包含一组 json 对象的 body 字段“到达”未定义。
编码:
//declaration of variable
tags = [{name: 'tag1'},{name: 'tag2'},{name: 'tag3'}];
//actual post
agent.post('/pictures')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/x-www-form-urlencoded')
.field('picTitle', 'Picture Title')
.field('tags', tags)
.attach('file', __dirname + '/img/noel.jpg')
.end(function(pictureSaveErr, pictureSaveRes) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
所以问题是服务器上的 req.body.tags 是未定义的。字符串没有问题。角度前端的实际实现运行良好,因此问题不在于服务器。
希望有人可以帮助我,在此先感谢...