如何用 Jest 测试 void 方法

How to test void method with Jest

我想测试记录器是否正确递增(使用 Jest)。查看下面的代码,这意味着我想检查是否调用了增量 (1)(表示 void myMethod 的内容)。

// myScript.ts
import dd from 'datadog-metrics';

export class MyClass { 
    private bufferedMetricsLogger: dd.BufferedMetricsLogger; 

    constructor(bufferedMetricsLogger: dd.BufferedMetricsLogger) { 
        this.bufferedMetricsLogger = bufferedMetricsLogger; 
    } 

    public myMethod(myInput: String): void { 
        this.bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${myInput}`]);  // (1)
    }
}

// myScript.test.ts
import { mock, instance, verify } from 'ts-mockito'; 
import { BufferedMetricsLogger } from 'datadog-metrics'; 
import { myClass } from 'path/to/myScript'; 

describe('Testing MyClass', () => { 
    it('verify that myMethod increments', () => { 
        const mockedBufferedMetricsLogger: BufferedMetricsLogger = mock(BufferedMetricsLogger); 
        const bufferedMetricsLogger:       BufferedMetricsLogger = instance(mockedBufferedMetricsLogger); 

        const mockMyClass: MyClass    = mock(MyClass); 
        const myClassOption1: MyClass = instance(mockMyClass); 
        const myClassOption2: MyClass = new MyClass(mockedBufferedMetricsLogger); 

        myClassOption1.myMethod('XXX');                                       // Option 1
        myClassOption2.myMethod('XXX');                                       // Option 2
        bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${XXX}`]);  // Option 3
        verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${XXX}`])).once();
    })
}); 

我尝试了三种不同的 Option。每个 Option 都会导致消息

Expected "increment(strictEqual(my.key.name)", strictEqual(1), strichtEqual([myTag:XXX]))" to be called 1 time(s). But has been called 0 time(s). 

实际上 Option 1Option 2 是我想要的。对于 Option 3 我刚刚尝试过,因为我认为这肯定有效。

有人可以帮我吗?

提前致谢。

您的代码中的问题是您将一个数组作为第三个参数传递,并且正如您在返回的消息中看到的那样,它正在尝试使用 strictEqual (===) 对其进行检查。你可以使用mockito提供的deepEqual函数得到想要的结果。

verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, deepEqual([`myTag:${XXX}`]))).once();

并确保您正确处理了参数,因为我不得不稍微更改代码以使其正常工作。这是我的完整工作代码:

// myScript.ts
import { BufferedMetricsLogger } from 'datadog-metrics';

export class MyClass { 
    private bufferedMetricsLogger: BufferedMetricsLogger; 

    constructor(bufferedMetricsLogger: BufferedMetricsLogger) { 
        this.bufferedMetricsLogger = bufferedMetricsLogger; 
    } 

    public myMethod(myInput: String): void {
        this.bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${myInput}`]);  // (1)
    }
}


// myScript.test.ts
import { mock, instance, verify, deepEqual } from 'ts-mockito'; 
import { BufferedMetricsLogger } from 'datadog-metrics'; 
import { MyClass } from './myScript'; 

describe('Testing MyClass', () => { 
    it('verify that myMethod increments', () => { 
        const mockedBufferedMetricsLogger: BufferedMetricsLogger = mock(BufferedMetricsLogger);
        const bufferedMetricsLogger:       BufferedMetricsLogger = instance(mockedBufferedMetricsLogger); 

        const myClassOption1 = new MyClass(bufferedMetricsLogger);
        // const bufferedMetricsLogger:       BufferedMetricsLogger = instance(mockedBufferedMetricsLogger); 

        // const mockMyClass: MyClass    = mock(MyClass); 
        // const myClassOption1: MyClass = instance(mockMyClass); 
        // const myClassOption2: MyClass = new MyClass(mockedBufferedMetricsLogger); 

        myClassOption1.myMethod('XXX');                                       // Option 1
        // myClassOption2.myMethod('XXX');                                       // Option 2
        // bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:XXX`]);  // Option 3
        verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, deepEqual([`myTag:XXX`]))).once();
    })
});