本文介绍了续集测试 - 有时验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Mocha/Chai 在 sequelize 定义上运行单元测试,如图所示:

I'm running unittests via Mocha / Chai on a sequelize definition as shown:

使用 mocha tests.js 运行的主要 tests.js:

Main tests.js that is run with mocha tests.js:

// Testing Dependencies
expect = require("chai").expect;
should = require("chai").should;
require('dotenv').load();

var Sequelize = require('sequelize');
var sequelize = new Sequelize(
    process.env.PG_DB_TEST,
    process.env.PG_USER,
    process.env.PG_PASSWORD, {
    dialect: "postgres",
    logging: false
});

var models = require('./models/db')(sequelize);

var seq_test = function (next) {
  return function () {
    beforeEach(function (done) {
        sequelize.sync({ force: true }).then(function() {
            done();
        });
    });

    afterEach(function (done) {
        sequelize.drop().then(function() {
            done();
        });
    });

    next();
  };
}

describe("Model Unittests", seq_test(function () {
  require("./models/tests/test_user.js")(models);
  require("./models/tests/test_interest.js")(models);
}));

test_user.js

test_user.js

var mockedUser = require("./mocks/user");

module.exports = function (models) {
  var User = models.user;
  it("User should have the correct fields", function (done) {
    User.create(mockedUser).then(function (result) {
      expect(result.pack()).to.include.all.keys(
            ["id", "name", "email", "intro"]
      );
      done();
    });
  });

  it("User should require an email", function (done) {
    User.create({
      "name": mockedUser['name']
    }).then(function (result) {
      expect.fail();
      done();
    }).catch(function (err) {
      expect(err['name']).to.be.equal('SequelizeValidationError');
      done();
    });
  });

  it("User should require a name", function (done) {
    User.create({
      "email": mockedUser['email']
    }).then(function (result) {
      expect.fail();
      done();
    }).catch(function (err) {
      expect(err['name']).to.be.equal('SequelizeValidationError');
      done();
    });
  });
}

有时(在 Codeship (CI) 上大约有 15 人中有 1 人)会出现此错误:

Sometimes (about 1 out of 15 on a Codeship (CI)), it gives this error:

  Model Unittests
Unhandled rejection SequelizeUniqueConstraintError: Validation error
at Query.formatError (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/sequelize/lib/dialects/postgres/query.js:402:16)
at null.<anonymous> (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/sequelize/lib/dialects/postgres/query.js:108:19)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at Query.handleError (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/pg/lib/query.js:108:8)
at null.<anonymous> (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/pg/lib/client.js:171:26)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at Socket.<anonymous> (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/pg/lib/connection.js:109:12)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
  1) "before each" hook for "User should have the correct fields"

在本地,这些单元测试并没有失败(我可能已经运行了它......连续 60 次).我之前在 beforeEachafterEach 中没有使用 done 回调时看到了类似的问题.这两个都是异步的,需要等待才能继续.修复该问题后,我在本地不再看到这些问题.

Locally, these unittests haven't failed (I've run it perhaps... 60 times in a row). I saw similar issues earlier when I didn't use the done callback in the beforeEach and afterEach. Both of those were async and needed to wait before continuing. After fixing that, I stopped seeing these issues locally.

谁能解释一下这个问题?(ssh 到 Codeship 并运行测试导致 1/~15 错误)

Can anyone shed some light on this issue? (ssh'ed into Codeship and ran the tests resulted in the 1 / ~15 error)

推荐答案

我遇到了这个问题.这是由于播种后未正确设置自动增量造成的.根本问题是我们的种子方法在种子方法中明确设置了主/自动增量键(id),通常应该避免这种情况.我们删除了 ID,问题得到了解决.

I had this issue. It was caused by the autoincrement not being set correctly after seeding. The root issue was that our seed methods were explicitly setting the primary/autoincremented key (id) in the seed method, which should generally be avoided. We remove the ids, and the issue was resolved.

这里是我们找到解决方案的续集问题的参考:https://github.com/sequelize/sequelize/issues/9295#issuecomment-382570944

Here's a reference to the sequelize issue where we found the solution: https://github.com/sequelize/sequelize/issues/9295#issuecomment-382570944

这篇关于续集测试 - 有时验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:05