2

我想制作该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方法。

4

1 回答 1

1

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

于 2017-04-04T12:05:06.780 回答