【问题标题】:mocha - send json to post express routemocha - 发送 json 来发布快递路线
【发布时间】:2014-04-01 00:20:17
【问题描述】:

我正在尝试在 mocha 测试中将一些 json 数据发送到我的 '/sign_up' 路由。

request = require 'supertest'
express = require 'express'
app = express()
Authentication = require("#{specDir}/../apps/authentication/routes")
authenticate = new Authentication app

Factory = require "#{specDir}/factories/user"
user = Factory.build 'user'
  it 'creates an account', (done) ->
    request(app).post('/sign_up').set('Accept', 'application/json').send(user).end (err, res) ->
      expect(res.statusCode).to.equal 200
      done()

但是回调函数中的req.bodyundefined。下面我展示了我的路由回调的 sn-p

  @app.post '/sign_up', (req, res) ->
    res.format
      html: -> res.status(406).send 'invalid Content-Type'
      json: ->
        console.log req.body
        res.status(200).send status: 'ok'

可能我遗漏了一些小细节,但看不到什么......有什么想法吗?

附:我很清楚测试通过了,它做了它应该做的,但在我继续编写更多测试之前,我必须知道如何发送一些数据。

【问题讨论】:

    标签: json node.js post express mocha.js


    【解决方案1】:

    您缺少正文解析器,请在代码中的某处添加 app.use(express.json())

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多