egg.js 在 test 实例中 无法调用 service 中使用的 this.app.mysql.insert
a.test.js
it('添加 usermeta', async () => {
// 创建 ctx
const ctx = app.mockContext()
// 通过 ctx 访问到 service.test
const test = await ctx.service.test.insert()
assert(test)
})
service/a.js
async insert() {
console.log('test', this.app.mysql.insert) // undefined
const result = await this.app.mysql.insert('table', {test:1})
return result.affectedRows === 1
}
error
TypeError: this.app.mysql.insert is not a function
如果在controller中是可以成功使用它的,但在test的情况下,mysql方法下没有insert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
unittest 环境中的数据库配置应该写到 config.default.js 中去。