6

我想测试记录器是否正确递增(使用 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的 s。每个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我只是尝试过,因为我认为这肯定会奏效。

有人可以帮我吗?

提前致谢。

4

1 回答 1

1

您的代码中的问题是您将数组作为第三个参数传递,并且正如您在返回的消息中看到的那样,它正在尝试使用 strictEqual (===) 来检查它。deepEqual您可以使用mockito 提供的功能获得所需的结果。

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();
    })
}); 
于 2020-08-19T08:43:23.693 回答