如何使用mocha.js模拟单元测试的依赖类?

时间:2022-11-07 18:34:51

Given that I have two ES6 classes.

鉴于我有两个ES6课程。

This is class A:

这是A类:

import B from 'B';

class A {
    someFunction(){
        var dependency = new B();
        dependency.doSomething();
    }
}

And class B:

和B级:

class B{
    doSomething(){
        // does something
    }
}

I am unit testing using mocha (with babel for ES6), chai and sinon, which works really great. But how can I provide a mock class for class B when testing class A?

我是使用mocha进行单元测试(使用babel for ES6),chai和sinon,它的效果非常好。但是,在测试A类时,如何为B类提供模拟类?

I want to mock the entire class B (or the needed function, doesn't actually matter) so that class A doesn't execute real code but I can provide testing functionality.

我想模拟整个类B(或所需的函数,实际上并不重要),以便A类不执行实际代码,但我可以提供测试功能。

This is, what the mocha test looks like for now:

这就是现在的mocha测试:

var A = require('path/to/A.js');

describe("Class A", () => {

    var InstanceOfA;

    beforeEach(() => {
        InstanceOfA = new A();
    });

    it('should call B', () => {
        InstanceOfA.someFunction();
        // How to test A.someFunction() without relying on B???
    });
});

1 个解决方案

#1


25  

You can use SinonJS to create a stub to prevent the real function to be executed.

您可以使用SinonJS创建存根以防止执行实际功能。

For example, given class A:

例如,给定A类:

import B from './b';

class A {
    someFunction(){
        var dependency = new B();
        return dependency.doSomething();
    }
}

export default A;

And class B:

和B级:

class B {
    doSomething(){
        return 'real';
    }
}

export default B;

The test could look like:

测试看起来像:

describe("Class A", () => {

    var InstanceOfA;

    beforeEach(() => {
        InstanceOfA = new A();
    });

    it('should call B', () => {
        sinon.stub(B.prototype, 'doSomething', () => 'mock');
        let res = InstanceOfA.someFunction();

        sinon.assert.calledOnce(B.prototype.doSomething);
        res.should.equal('mock');
    });
});

You can then restore the function if necessary using object.method.restore();:

然后,您可以使用object.method.restore();在必要时恢复该功能:

var stub = sinon.stub(object, "method");
Replaces object.method with a stub function. The original function can be restored by calling object.method.restore(); (or stub.restore();). An exception is thrown if the property is not already a function, to help avoid typos when stubbing methods.

var stub = sinon.stub(object,“method”);用stub函数替换object.method。可以通过调用object.method.restore()来恢复原始函数; (或stub.restore();)。如果属性不是函数,则抛出异常,以帮助避免在存根方法时出现拼写错误。

#1


25  

You can use SinonJS to create a stub to prevent the real function to be executed.

您可以使用SinonJS创建存根以防止执行实际功能。

For example, given class A:

例如,给定A类:

import B from './b';

class A {
    someFunction(){
        var dependency = new B();
        return dependency.doSomething();
    }
}

export default A;

And class B:

和B级:

class B {
    doSomething(){
        return 'real';
    }
}

export default B;

The test could look like:

测试看起来像:

describe("Class A", () => {

    var InstanceOfA;

    beforeEach(() => {
        InstanceOfA = new A();
    });

    it('should call B', () => {
        sinon.stub(B.prototype, 'doSomething', () => 'mock');
        let res = InstanceOfA.someFunction();

        sinon.assert.calledOnce(B.prototype.doSomething);
        res.should.equal('mock');
    });
});

You can then restore the function if necessary using object.method.restore();:

然后,您可以使用object.method.restore();在必要时恢复该功能:

var stub = sinon.stub(object, "method");
Replaces object.method with a stub function. The original function can be restored by calling object.method.restore(); (or stub.restore();). An exception is thrown if the property is not already a function, to help avoid typos when stubbing methods.

var stub = sinon.stub(object,“method”);用stub函数替换object.method。可以通过调用object.method.restore()来恢复原始函数; (或stub.restore();)。如果属性不是函数,则抛出异常,以帮助避免在存根方法时出现拼写错误。