测试子类时,如何监视祖父母类中的导入函数?

时间:2019-04-19 12:59:20

标签: javascript node.js class jasmine sinon

我无法监视从节点模块导入的功能。我正在测试一个子类,并将模块导入到一个祖父母类中,我需要查看该函数使用了哪些参数。

代码按预期工作,但是我已经使用Jasmine内置的spyOnsinon.spy进行了测试,但是没有调用任何间谍。

代码:

// ChildClass.js

const ParentClass = require('./ParentClass');

module.exports = class ChildClass extends ParentClass {

    constructor () {
        super();
        this.foo();
    }

    foo () {
        this.message = this.importGetter('Some input');
    }

};
// ParentClass.js

const GrandparentClass = require('./GrandparentClass');

module.exports = class ParentClass extends GrandparentClass {

    constructor () {
        super();
        this._message = null;
    }

    get message () {
        return this._message;
    }

    set message (value) {
        this._message = value;
    }

};
// GrandparentClass.js

const nodeModuleFunction = require('./nodeModule').nodeModuleFunction;

module.exports = class GrandparentClass {

    get importGetter () {
        return nodeModuleFunction;
    }

};
// nodeModule.js

async function nodeModuleFunction (input) {

    console.error('Do something with input: ', input);

    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Returned from node module.');
        }, 300);
    });
}

exports.nodeModuleFunction = nodeModuleFunction;

测试代码:

// test.spec.js

const ChildClass = require('./ChildClass');
const nodeModule = require('./nodeModule');
const sinon = require('sinon');

describe('ChildClass test', () => {

    describe('Jasmine spy', () => {

        it('should call nodeModule.nodeModuleFunction with given value', done => {

            spyOn(nodeModule, 'nodeModuleFunction').and.callThrough();

            const object = new ChildClass();

            expect(object.message).not.toBeNull();
            expect(nodeModule.nodeModuleFunction).toHaveBeenCalled();
            expect(nodeModule.nodeModuleFunction).toHaveBeenCalledWith('Some input');

            object.message.then(message => {
                expect(message).toBe('Returned from node module.');
                done();
            });

        });

    });

    describe('Sinon spy', () => {

        it('should call nodeModule.nodeModuleFunction with given value', done => {

            const spy = sinon.spy(nodeModule, 'nodeModuleFunction');

            const object = new ChildClass();

            expect(object.message).not.toBeNull();
            expect(spy.called).toBe(true);
            expect(spy.withArgs('Some input').calledOnce).toBe(true);

            object.message.then(message => {
                expect(message).toBe('Returned from node module.');
                done();
            });
        });

    });

});

测试结果:

Jasmine started
Do something with input:  Some input

  ChildClass test

    Jasmine spy
      ✗ should call nodeModule.nodeModuleFunction with given value
        - Expected spy nodeModuleFunction to have been called.
        - Expected spy nodeModuleFunction to have been called with [ 'Some input' ] but it was never called.

Do something with input:  Some input
    Sinon spy
      ✗ should call nodeModule.nodeModuleFunction with given value
        - Expected false to be true.
        - Expected false to be true.

按照Brian的建议使用解决方案进行编辑:

const nodeModule = require('./nodeModule');

describe('ChildClass test', () => {

    let ChildClass;

    beforeAll(() => {
        spyOn(nodeModule, 'nodeModuleFunction').and.callThrough();  // create the spy...
        ChildClass = require('./ChildClass');  // ...and now require ChildClass
    });

    afterEach(() => {
        nodeModule.nodeModuleFunction.calls.reset();
    });

    describe('Jasmine spy', () => {

        it('should call nodeModule.nodeModuleFunction with given value', done => {

            const object = new ChildClass();

            expect(object.message).not.toBeNull();
            expect(nodeModule.nodeModuleFunction).toHaveBeenCalled();
            expect(nodeModule.nodeModuleFunction).toHaveBeenCalledWith('Some input');

            object.message.then(message => {
                expect(message).toBe('Returned from node module.');
                done();
            });

        });

        it('should still call nodeModule.nodeModuleFunction with given value', done => {

            const object = new ChildClass();

            expect(object.message).not.toBeNull();
            expect(nodeModule.nodeModuleFunction).toHaveBeenCalled();
            expect(nodeModule.nodeModuleFunction).toHaveBeenCalledWith('Some input');

            object.message.then(message => {
                expect(message).toBe('Returned from node module.');
                done();
            });

        });

    });

});
const nodeModule = require('./nodeModule');
const sinon = require('sinon');

describe('ChildClass test', () => {

    let spy;
    let ChildClass;

    beforeAll(() => {
        spy = sinon.spy(nodeModule, 'nodeModuleFunction');  // create the spy...
        ChildClass = require('./ChildClass');  // ...and now require ChildClass
    });

    afterEach(() => {
        spy.resetHistory();
    });

    afterAll(() => {
        spy.restore();
    });

    describe('Sinon spy', () => {

        it('should call nodeModule.nodeModuleFunction with given value', done => {

            const object = new ChildClass();

            expect(object.message).not.toBeNull();
            expect(spy.called).toBe(true);
            expect(spy.withArgs('Some input').calledOnce).toBe(true);

            object.message.then(message => {
                expect(message).toBe('Returned from node module.');
                done();
            });
        });

        it('should still call nodeModule.nodeModuleFunction with given value', done => {

            const object = new ChildClass();

            expect(object.message).not.toBeNull();
            expect(spy.called).toBe(true);
            expect(spy.withArgs('Some input').calledOnce).toBe(true);

            object.message.then(message => {
                expect(message).toBe('Returned from node module.');
                done();
            });
        });

    });

});

1 个答案:

答案 0 :(得分:1)

GrandparentClass.js需要nodeModule.js并在运行时立即获取对nodeModuleFunction 的引用 ...

...所以您只需要确保间谍在运行前 就可以了:

const nodeModule = require('./nodeModule');
const sinon = require('sinon');

describe('ChildClass test', () => {

  describe('Sinon spy', () => {

    it('should call nodeModule.nodeModuleFunction with given value', done => {

      const spy = sinon.spy(nodeModule, 'nodeModuleFunction');  // create the spy...
      const ChildClass = require('./ChildClass');  // ...and now require ChildClass

      const object = new ChildClass();

      expect(object.message).not.toBeNull();  // Success!
      expect(spy.called).toBe(true);  // Success!
      expect(spy.withArgs('Some input').calledOnce).toBe(true);  // Success!

      object.message.then(message => {
        expect(message).toBe('Returned from node module.');  // Success!
        done();
      });
    });

  });

});

我更新了Sinon测试,但该方法也适用于Jasmine测试。