运行testing之前清除testing数据库
在运行testing套件之前清理数据库的最好方法是什么(是否有npm库或推荐的方法)。
我知道before()函数。
我使用节点/快递,摩卡和续集。
before
函数和清理数据库的function差不多。 如果您只需要清理一次数据库,即在运行所有testing之前,您可以在一个单独的文件中使用一个全局的before
函数
globalBefore.js
before(function(done) { // remove database data here done() })
单testing1.js
require('./globalBefore) // actual test 1 here
单testing2.js
require('./globalBefore) // actual test 2 here
请注意,globalBefore将只运行一次,即使它已被需要两次
testing原理
尝试限制在testing中使用外部依赖性,如数据库。 外部依赖性越小,testing越容易。 您希望能够并行运行所有的unit testing,并且像数据库这样的共享资源会使这种情况变得困难。
看看这个Google技术讨论关于编写可testing的JavaScript http://www.youtube.com/watch?v=JjqKQ8ezwKQ
再看看rewire模块。 它可以很好地用于保存function。
我通常这样做(比如User
模型):
describe('User', function() { before(function(done) { User.sync({ force : true }) // drops table and re-creates it .success(function() { done(null); }) .error(function(error) { done(error); }); }); describe('#create', function() { ... }); });
还有sequelize.sync({force: true})
,它将删除并重新创build所有表( .sync()
在这里描述)。
我做了这个库清理和import夹具为您的testing。
这样,你可以导入灯具,testing然后清理你的数据库。
看看下面的内容:
before(function (done) { prepare.start(['people'], function () { done(); }); }); after(function () { prepare.end(); });