我想制作该getDbmodel
方法的存根。
它被定义在basemodel.js
其中
import Sequelize from 'sequelize';
import db from '../../config/database';
let instance = null;
class Database {
static getDbmodel = () => {
if (instance != null) {
return instance;
}
instance = new Sequelize(db.default.database, db.default.username, db.default.password, {
host: db.default.host,
dialect: db.default.dialect,
define: {
timestamps: false, // true by default
},
pool: {
max: db.default.maxConnections,
min: db.default.minConnections,
idle: db.default.idleTimeoutMillis,
timezone: '+00:00',
},
});
console.log('inside basemodel');
return instance;
}
}
export { Database as default };
// ========================================================
// src/Libraries/ABTest.js
import ABTestStorage from '../Storage/ABTest';
class ABTestLib {
getABTestById = id => ABTestStorage.getABTestById(id);
getABTests = () => ABTestStorage.getABTests().then()
.catch(err => console.log(err))
save = abtestObj => ABTestStorage.saveABTest(abtestObj).then()
.catch(err => console.log(err))
put = abtestObj => ABTestStorage.updateABTest(abtestObj)
.then()
.catch(err => console.log(err))
delete = id => ABTestStorage.destroyABTestById(id).then()
.catch(err => console.log(err))
}
export default ABTestLib;
abtest.js
导入导入的ab
测试模型basemodel
。
这是我的测试文件:
import chai from 'chai';
const SequelizeMock = require('sequelize-mock');
var proxyquire = require('proxyquire');
describe('Libraries/ABTest.js', () => {
before(function () {
let instance = null;
class Database {
static getDbmodel = () => {
if (instance != null) {
return instance;
}
instance = new SequelizeMock();
console.log(instance);
return instance;
}
}
});
var stubs = {
'../..src/Libraries/BaseModel': {
getDbmodel: function () {
console.log('3444e4');
if (instance != null) {
console.log('1111');
return instance;
}
instance = new SequelizeMock();
console.log(instance);
return instance;
}
}
};
const ABTests = proxyquire('../../src/Libraries/ABTest', stubs);
console.log(ABTests);
const abtest = new ABTests.default();
it('get(1): should return response for a abtest', () => {
abtest.getABTestById(1).then((res) => {
chai.expect(res).to.be.an('object');
});
});
it('getAll() : should return response and status 200 for all abtests', () => {
abtest.getABTests().then((res) => {
chai.expect(res).to.be.an('object');
});
});
it('save() : should return response and status 200 for a abtest', () => {
abtest.save({ id: '500', name: 'home', code: 'home', id_organization: '1', id_platform: '1', }).then((res) => {
chai.expect(res).to.be.an('object');
});
});
it('put() : should return response and status 200 for a abtest', () => {
abtest.put({ id: '2', }).then((res) => {
chai.expect(res).to.be.an('object');
});
});
it('delete() : should return response and status 200 for a abtest', () => {
abtest.delete({ id: '111', }).then((res) => {
chai.expect(res).to.be.an('object');
});
});
});
为什么它总是进入basemodels
gedbmodel
而不是被嘲笑的?
我想模拟该getdbmodel
方法。