Mocha测试,在每个文件运行之前清理磁盘数据库 [英] Mocha tests, clean disk database before every file runs

查看:106
本文介绍了Mocha测试,在每个文件运行之前清理磁盘数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sails 1.x.

I am using Sails 1.x.

是否可以在每个测试文件运行之前重置Sails.js数据库?我希望它在每次运行之前在sails.lift()完成之后处于状态.我在这里关注了文档- https://sailsjs.com/documentation/concepts/testing -但最终并没有得到像这样的解决方案.

Is it possible to reset the Sails.js database before each test file runs? I want it to be in state after sails.lift() completes before each run. I followed the docs here - https://sailsjs.com/documentation/concepts/testing - but did not end up with any solution like this.

我目前唯一的解决方案是将lifecyle.test.js beforeafter更改为运行beforeEveryafterEvery-

The only solution I'm having right now is to change the lifecyle.test.js before and after to run beforeEvery and afterEvery - https://sailsjs.com/documentation/concepts/testing - so this is lifting everytime before test. But it takes a long time to lift.

推荐答案

这很简单.您只需要指定在数据源的连接中添加测试连接(取决于Sails.js的版本),在测试期间将其设置为活动状态,并提供迁移策略'drop'即可在每次启动时重新构建数据库

This is very simple to do. You just need to specify to add test connection in your connections on datasourses (depends on the version of Sails.js), setup it as active during the test and provide migration strategy 'drop' which is just rebuild your DB every time on startup

models: {
    connection: 'test',
    migrate: 'drop'
},

我的连接Sails.js 0.12.14

My connections Sails.js 0.12.14

module.exports.connections = {
  prod: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    database: 'some-db'
  },

  test: {
    adapter: 'sails-memory'
  },
};

我简化的lifecycle.test.js

My simplified lifecycle.test.js

let app;
// Before running any tests...
before(function(done) {
    // Lift Sails and start the server
    const Sails = require('sails').constructor;

    const sailsApp = new Sails();
    sailsApp.lift({
        models: {
            connection: 'test',
            migrate: 'drop'
        },
    }, function(err, sails) {
        app = sails;
        return done(err, sails);
    });
});

// After all tests have finished...
after(async function() {
    // here you can clear fixtures, etc.
    // (e.g. you might want to destroy the records you created above)

    try {
        await app.lower()
    } catch (err) {
        await app.lower()
    }
});

在Sails 1中,它甚至更简单

In Sails 1 it's even simpler

const sails = require('sails');

before((done) => {
  sails.lift({
    datastores: {
      default: {
        adapter: 'sails-memory'
      },
    },
    hooks: { grunt: false },
    models: {
      migrate: 'drop'
    },
  }, (err) => {
    if (err) { return done(err); }

    return done();
  });
});

after(async () => {

  await sails.lower();

});

这篇关于Mocha测试,在每个文件运行之前清理磁盘数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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