Nodejs模拟一个方法

时间:2017-04-04 11:29:20

标签: node.js proxyquire

我想制作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的{​​{1}}测试模型。

这是我的测试文件:

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

1 个答案:

答案 0 :(得分:1)

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