【发布时间】:2014-04-15 06:07:35
【问题描述】:
我正在使用 Express 4.0.0 创建一个 API,其中一条路由需要一个 POST。目前我只是想让它回显在请求参数中发送的名称。响应是 JSON 对象,但请求需要表单字段。
users.post '/user', (req, res) ->
res.json name: req.params.name
我的测试将type() 设置为form,这应该允许send() 将哈希作为POST 参数/字段传递。
describe 'POST /user', ->
it 'should echo the name sent', (done) ->
request app
.post '/user'
.type 'form'
.send name: 'Foo'
.expect '{"name":"Foo"}'
.end done
无论如何,我的测试失败了,在 Express 中,我的 req.params 是空的,req.param('name') 出现 undefined 和 req.body 也是空的。
是否有一些我不知道的req.fields 属性,或者我的测试存在某种缺陷?
【问题讨论】:
-
你在使用快速正文解析器吗?
-
不,我没有使用它。
-
你的快递是什么版本的?
-
Express 4.0.0,我也用这个细节更新了这个问题。
标签: node.js http testing express supertest