我正在使用 mocha、supertest 和 assert 来测试我的 Express 应用程序。我的 Express 应用程序在开发模式下运行,因此每当请求失败时,它会返回有用的调试信息作为 JSON。我想在测试套件中打印这些数据,但仅在测试失败时。以下是我的一个测试示例(使用 CoffeeScript):
assert = require "assert"
request = require "supertest"
url = request "http://localhost:3000"
describe "GET /user/:id", ->
it "should return one user", (done) ->
url
.get("/user" + id)
.expect(200)
.expect("Content-Type", /json/)
.end (err, res) ->
if err
done err
else
# assuming the test reaches here, but fails on one of the following,
# how do i make mocha print res.body?
assert.equal(res.body.name, user.name)
assert.equal(res.body.email, user.email)
done()
我该如何让mocha打印出res.body,但仅当测试失败时?如果可能的话,我希望不必在每个"describe"块中都放置类似于console.log(res.body) if test.failed
的东西。