我很难弄清楚如何正确测试这段代码:
const chai = require('chai')
const chaiHttp = require('chai-http')
const restify = require('restify')
const errs = require('restify-errors')
const expect = chai.expect
chai.use(chaiHttp)
const willerror = true
const server = restify.createServer()
server.get('/', function (req, res, next) {
if (!willerror) res.send()
next(willerror ? new errs.InternalServerError() : null)
})
describe('Server test', function () {
before(async function () {
const c = {config: {dev: {driver: 'sqlite3', filename: ':memory:'}}}
const dbm = require('db-migrate').getInstance(true, c)
dbm.silence(true)
await dbm.reset().then(() => dbm.up('all', 'all'))
})
after(function (done) {
server.close(done)
})
it('posts', function (done) {
const client = chai.request(server)
client.get('/')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
done()
})
})
})
(注意:它至少需要一次迁移,但它可以为空:db-migrate create one
应该足够了。)
我得到的输出:
删除before()
来电的内容(仅留下before(async function(){})
)会给出我所期望的内容:
为什么这些测试没有完成?
答案 0 :(得分:0)
原来this。
要打败它,请将throwUncatched: true
添加到传递给db-migrate
的配置对象中。就我而言:
const c = {throwUncatched: true, config: {dev: {driver: 'sqlite3', filename: ':memory:'}}}