擦除模式中的所有表 - sequelize nodejs [英] Wipe all tables in a schema - sequelize nodejs

查看:35
本文介绍了擦除模式中的所有表 - sequelize nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用 sequelize 作为 ORM 在 mysql 数据库上执行单元测试,我需要在每个测试开始运行时刷新我的数据库.其实我写了一个这样的解决方案:

To perform unit testing on a mysql database using sequelize as ORM, I need to refresh my database when each test start to run. Actually I wrote a solution like this:

beforeEach(() => {
   table1.destroy({ where: {} })
   table2.destroy({ where: {} })
   table3.destroy({ where: {} })
})

但每次我创建表格时,我都必须添加另一条指令.我将实现一条指令来执行整个架构的完整擦除

but every time I create tables I have to add another instruction. I would implement a single instruction to perform a complete wipe of the entire schema

类似于:

beforeEach(() => db.clean())

推荐答案

在进行单元测试时不应该接触数据库.

You should not touch the database when doing unit testing.

如果您正在测试与 sequelize 对话的业务逻辑,请创建一个模拟接口以进行 sequelize 并将其注入正在测试的单元中.然后,您可以对模拟接口的方法的调用做出断言或期望.如果没有关于测试环境的更多细节,就不可能提供比这更具体的方向,但您可以调查 sequelize-mocking 包来促进这一点.

If you're testing business logic that talks to sequelize, create a mock of the interface to sequelize and inject that into the unit being tested. You can then make assertions or expectations with regard to calls to the mock interface's methods. Without more details of the testing environment, it's impossible to provide more concrete direction than that, but you might investigate the sequelize-mocking package to facilitate that.

如果您正在测试 sequelize 实际上是在与您的数据库对话,那么您所做的不仅仅是单元测试,而且我认为您需要一种带外初始化和管理环境的方式,但我还要指出 sequelize 已经有一整套测试.

If you're testing that sequelize is actually talking to your database, then you are doing a whole lot more than unit testing, and I would argue that you would want an out-of-band way of initializing and managing the environment, but I'd also point out that sequelize has a whole suite of tests already.

这篇关于擦除模式中的所有表 - sequelize nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆