sih*_*hrc 5 javascript unit-testing mocha.js sequelize.js
我正在通过Mocha/Chai在续集定义上运行单元测试,如下所示:
运行的主要tests.js 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);
}));
Run Code Online (Sandbox Code Playgroud)
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();
});
});
}
Run Code Online (Sandbox Code Playgroud)
有时(Codeship(CI)中的15个中约有1个),它会出现此错误:
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"
Run Code Online (Sandbox Code Playgroud)
在本地,这些单元测试没有失败(我已经连续运行了60次).当我没有done在beforeEach和中使用回调时,我之前看到过类似的问题afterEach.这两个都是异步的,需要等待才能继续.解决之后,我在本地停止了这些问题.
任何人都可以对这个问题有所了解吗?(ssh进入Codeship并运行测试导致1/~15错误)
我的 QA 数据库遇到了这个问题。有时新记录会保存到数据库,有时会失败。当在我的开发工作站上执行相同的过程时,每次都会成功。
当我捕获错误并将完整结果打印到控制台时,它确认违反了唯一约束 - 具体来说,主键 id 列被设置为默认为自动增量值。
我在数据库中添加了记录,尽管这些记录的 id 也设置为自动增量,但 200 多条记录的 id 分散在 1 到 2000 之间,但数据库的自动增量序列设置为从 1 开始。通常序列中的下一个 id 未被使用,但有时它已经被占用,数据库会返回此错误。
我使用此处的答案将序列重置为在最后一个种子记录之后开始,现在它每次都有效。
如果您正在播种记录以运行集成测试,则数据库自动增量序列可能未设置为遵循它们。Sequelize 没有此功能,因为它是一个简单的单命令操作,需要在数据库中运行。