sme*_*ten 1 arrays request mocha.js node.js supertest
我正在用 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 是未定义的。字符串没有问题。角度前端的实际实现运行良好,因此问题不在于服务器。
希望有人可以帮助我,在此先感谢...
似乎什么field方法不接受数组。因为它在引擎盖下使用了表单数据模块。
你应该尝试这样的事情:
agent.post('/pictures')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/x-www-form-urlencoded')
.field('picTitle', 'Picture Title')
.field('tags[0][name]', tags[0].name)
.field('tags[1][name]', tags[1].name)
.field('tags[2][name]', tags[2].name)
.attach('file', __dirname + '/img/noel.jpg')
.end(function(pictureSaveErr, pictureSaveRes) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3271 次 |
| 最近记录: |