【发布时间】:2017-01-01 01:02:10
【问题描述】:
我是 node 新手,我只是在尝试编写一个简单的后端博客 API。我正在使用 bookshelf.js 作为 ORM,并且我正在尝试使用 bookshelf-validate 来强制执行我制作的文章模型的要求。我在 Article 模型中包含的验证仅仅是对所有字段(字段为标题、作者和正文)的 isRequired 验证。我的一个测试创建了一篇定义了所有字段的新文章,但测试失败了。这是我的代码,
//here is the bookshelf model
const Bookshelf = require('../config/bookshelf.config');
const Article = Bookshelf.Model.extend({
tableName: 'articles',
hasTimestamps: true,
validations: {
title: {
isRequired: true
},
author: {
isRequired: true
},
body: {
isRequired: true
}
}
});
module.exports = Bookshelf.model('Article', Article);
//test file below
process.env.NODE_ENV = 'test';
const chaiAsPromised = require('chai-as-promised');
const { expect, assert } = require('chai').use(chaiAsPromised);
const knex = require('knex')(require('../knexfile')[process.env.NODE_ENV]);
const Article = require('../models/article');
describe('Articles', function () {
beforeEach(function () {
return knex.migrate.rollback()
.then(function () {
return knex.migrate.latest();
});
});
after(function () {
return knex.migrate.rollback();
});
describe('test db', function () {
it('should not have any models at start of test suite', function () {
Article.forge().fetch().then(function (results) {
expect(results).to.equal(null);
});
});
it('should save a model to the db', function () {
const article = new Article({
title: 'first blog',
author: 'john doe',
body: 'blah blah'
}).save();
return expect(article).to.be.fulfilled;
});
});
});
这里也是要点https://gist.github.com/Euklidian-Space/bf10fd1a72bec9190867854d1ea309d9
提前致谢。
【问题讨论】:
标签: promise mocha.js bookshelf.js