如果我定义我的模型实体而没有像这样指定每个表列:
let bookshelf = require('../config/database');
require('./role');
var User = bookshelf.Model.extend({
tableName: 'users',
role: function() {
return this.hasOne(Role);
}
});
module.exports = bookshelf.model('user', User);
如何在不写入数据库的情况下创建我的Mocha / Sinon测试?
我尝试过这样一个简单且明显失败的测试:
var sinon = require('sinon');
var user = require('../../models/user');
var sandbox = sinon.sandbox.create();
describe('User model', function () {
beforeEach(function () {
sandbox.stub(user);
});
afterEach(function () {
// completely restore all fakes created through the sandbox
sandbox.restore();
});
it('should return empty set before adding anything', () => {
sinon.assert.callCount(user, 1);
});
});
然而,实际上我收到了一条错误消息:
User model
1) "before each" hook for "should return empty set before adding"
0 passing (41ms)
1 failing
1) User model "before each" hook for "should return empty set before adding":
TypeError: Attempted to wrap undefined property undefined as function
at wrapMethod (node_modules/sinon/lib/sinon/util/core/wrap-method.js:70:21)
at stub (node_modules/sinon/lib/sinon/stub.js:56:44)
at Object.stub (node_modules/sinon/lib/sinon/collection.js:87:33)
at Context.<anonymous> (test/models/User.spec.js:10:17)
var ModelsUser = require('../../models/user'),
User = ModelsUser.User,
Role = require('../../models/role'),
chai = require('chai'),
expect = chai.expect,
should = chai.should();
var assert = require('assert');
describe('User model', function () {
var test_usr = {name: 'Test User', username: 'test_usr', role_id: 1};
beforeEach((done) => {
// making sure the test user is removed before any test
User.query().del().then(() => done());
});
it('should return empty set before adding anything', (done) => {
User.forge().count().then(function(total) {
expect(total).to.equal(0);
done();
})
.catch(err => done(err));
});
it('should return a valid user after insert', (done) => {
User.forge(test_usr)
.save().then((model) => {
should.exist(model.id);
done();
})
.catch(err => done(err));
});
});