小编Jam*_*oop的帖子

使用Sequelize构建,播种和销毁PostgreSQL以进行测试

我正在尝试为每个测试自动化构建,播种和销毁我的数据库。我正在使用PostgreSQL,Mocha和Sequelize。

我找到了一个库:sequelize-fixtures,使我步入正轨,但最终它非常不一致,有时会引发约束错误:Unhandled rejection SequelizeUniqueConstraintError: Validation error即使我对模型没有任何验证。

这是我进行测试的方式

const sequelize = new Sequelize('test_db', 'db', null, {
  logging: false,
  host: 'localhost',
  port: '5432',
  dialect: 'postgres',
  protocol: 'postgres'
})

describe('/auth/whoami', () => {
  beforeEach((done) => {
    Fixtures.loadFile('test/fixtures/data.json', models)
      .then(function(){
         done()
      })
  })

  afterEach((done) => {
    sequelize.sync({
      force: true
    }).then(() => {
      done()
    })
  })

  it('should connect to the DB', (done) => {
    sequelize.authenticate()
      .then((err) => {
        expect(err).toBe(undefined)
        done()
      })
  })

  it('should test getting a user', (done) => …
Run Code Online (Sandbox Code Playgroud)

testing postgresql mocha.js node.js sequelize.js

5
推荐指数
1
解决办法
1254
查看次数

标签 统计

mocha.js ×1

node.js ×1

postgresql ×1

sequelize.js ×1

testing ×1