如何伪造Typescript名称空间的间接调用函数?

时间:2019-04-17 08:29:22

标签: typescript mocking mocha sinon

假定给出以下命名空间:

export namespace CodeUnderTest {
    export function A(): void {
        B();
    }

    export function B(): void {
        console.log("B was called.");
    }
}

如何编写使用A()的伪实现调用B()的测试?

这是我尝试与Sinon一起做的事情:

import * as sinon from 'sinon';
import { CodeUnderTest } from './code-under-test';

describe('CodeUnderTest', () => {
    it('calls fake', () => {
        sinon.replace(CodeUnderTest, 'B', () => {
            console.log("Fake was called.");
        });

        CodeUnderTest.A();
    });
})

当我使用mocha -r ts-node/register code-under-test.spec.ts运行它时,测试将打印B was called.,我希望它可以打印Fake was called.

我愿意接受任何解决方案,而不仅仅是用Sinon实现的解决方案。

0 个答案:

没有答案