gpt4 book ai didi

node.js - Nodejs 模拟方法

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:44 24 4
gpt4 key购买 nike

我想创建 getDbmodel 方法的 stub 。

它在 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 方法。

最佳答案

尝试在 proxyquire 中使用 noCallThru 函数: proxyquire.noCallThru().load('../../src/Libraries/ABTest', stubs).default

关于node.js - Nodejs 模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com